Repository navigation
Design Meeting Notes, 6/13/2018 #24940
Copy link
Copy link
Closed
Labels
Design NotesNotes from our design meetingsNotes from our design meetings
Description
Rest parameters and tuple types
-
In a nutshell, the type of a rest parameter can now be a tuple.
- Previously we said rest parameters could only be arrays.
- What this means is that an n-tuple-trailed rest parameter (i.e.
(arg0: boolean, ...args: [number, string])) is the same as having n trailing parameters (i.e.(arg0: boolean, arg1: number, arg2: string)).
-
Because we need to encode everything that parameter lists have, we need a few things: empty length, optionality and open-endedness parameters
- Now have an empty tuple types.
- Also now allowing tuple elements to be optional using
?.- Means that the length becomes a union of
- Is the
?part of the type?- No, it's a modifier for tuple elements
- We said last week we'd be okay giving the syntax up for this case.
- In that context.
- And plus, we already recognize this in JSDoc anyway, so we've already effectively given it up.
- Rest args mean we need open-ended tuples.
-
Currently can't operate on generic type argument lists.
-
All methods are generic on the called function (they use a
thisparameter to infer the parameter types of the functions themselves.)- Can
Functionitself just be made generic instead of doing this?- Well, doesn't work with overloads.
- Well there are just going to be things that don't work.
- Can
-
What doesn't work?
-
Inferring from a generic argument list:
function call<T extends unknown[], U>(f: (...args: T) => U, ...args: T) { f() }
-
Conditional type relationships
- Today, a type isn't assignable to a conditional type unless it's another conditional type.
- Makes it hard to actually implement something that returns a conditional type.
- We also have what are called "substitution types": constraints that are built up in every conditional type's
extendscheck.- If you're in the
falsebranch, this PR builds up negated constraints.
- If you're in the
- To perform assignability checks, we first assume that
- But a conditional type can give you a false positive for when the runtime type is more derived.
- What about
T extends T ? ... : ...- Should still work.
- Will have to think about this.
- Mixed assuredness
More conditional type relationships
- Use conditional type to narrow return type of function with a union-typed argument #24929
- Seems okay, but only because they're disjoint.
Also, how far do you want to go?
function - We want to take baby steps.
- One good baby step - if you're assignable to both branches, it should just work.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
Design NotesNotes from our design meetingsNotes from our design meetings
Activity
ajafff commentedon Jun 14, 2018
How do open ended tuples work w.r.t. their length property. Is this an infinite union? Is this something range types (#15480) would be useful for?
mhegazy commentedon Jun 14, 2018
it is a
number.