Add Term Type #361
Labels
No Label
a11y
Backlog
Blocked
Bug
Content
Dependencies
Design
Feature Request
Good First Issue
In Progress
Performance
Priority - High
Priority - Low
Priority - Medium
Untriaged
No Milestone
No project
No Assignees
3 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: www/www-new#361
Loading…
Reference in New Issue
No description provided.
Delete Branch "j285he-add-term-type"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
I think problem with
generate-calendar
not working is that it importsgetAllEvents
which is in the filelib/events.ts
that imports the new type from@/utils
. Sincegenerate-calendar.ts
is ats-node
file the paths/aliases intsconfig
don't work (https://github.com/TypeStrong/ts-node/issues/138)Some solutions may be
events.ts
, use../utils
instead of@/utils
-- we could do this for all lib files to make things consistentOr may be I'm missing something. What do you think?
@ -1,3 +1,11 @@
export const TERMS = ["winter", "spring", "fall"] as const;
export type Term = typeof TERMS[number];
As a noob, I'm curious: what's the advantage to defining the
Term
type this way, instead of using a union of strings ("winter" | "spring" | "fall"
)?I saw other files (
lib/events
andlib/news
) specifically definingTERMS = ["winter", "spring", "fall"]
and I thought it would be useful to move that intoutils
. More generally, if we were to change whatTERMS
is, say if "spring" was renamed to "summer", then we would only need one code change here (although that's probably never going to happen).Term = typeof TERMS[number]
is the syntax to transform the const array to type"winter" | "spring" | "fall"
.@ -1,0 +3,4 @@
// https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates
export function isTerm(x: string): x is Term {
return x === "winter" || x === "spring" || x === "fall";
Is there a reason for this approach instead of
return TERMS.includes(x);
?Because I used
as const
onTERMS
, I believe TypeScript infers the "narrowest type", so TypeScript identifiesTERMS
as a readonly array of["winter", "spring", "fall"]
instead of an array of strings. Therefore usingreturn TERMS.includes(x)
gives me the errorArgument of type 'string' is not assignable to parameter of type '"winter" | "spring" | "fall"'
.I think the reasoning for using
as const
to get the "narrowest type" is so that we can define the typeTerm
using the arrayTERMS
. Ifas const
was not present thenTerm
would just be typestring[]
(which is not specific enough for our purposes).Let me know if there's a way to improve this. I agree it does look clunky
Ahh okay that's really interesting, thanks for the explanation!
Personally, I think the way this is written right now is actually quite readable. My only concern was if we ever wanted to, say, change "spring" to "summer", then we would have to make the change in two separate places (albeit two places in the same file).
I just did some experimenting and it seems like
return TERMS.some(term => x === term);
could work - this solves the "change in two separate places" issue, but it's also a tad less readable in my opinion. I'll leave it up to you to decide what to use!Good point and good idea. Wonder if anyone else has thoughts or opinions on this.
I liked the implementation and changed it. Let me know if anyone wants it reversed.
Our CI does highlight type errors with the new custom Term type, see
5a87174b13
vs95287b4de7
.Looks great, thanks for this!
Sorry for the very slow review
@ -12,4 +11,0 @@
): Promise<Member[]> {
if (term !== "winter" && term !== "spring" && term !== "fall") {
throw new Error(`[getMembers] Not a valid term: "${term}"`);
}
Nice!