You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The ability to take a generic function and supply type arguments and instantiate the signatures.
You could've done this with a type assertion: f as (some: Type, params: OtherType) => RetType
Often cumbersome.
Also more error-prone.
This works across both functions and constructor functions.
From the JS emit perspective, this is the same as if you'd omitted type arguments completely.
This gets rid of an unnecessary pattern people used where they'd create a subclass to specialize a constructor.
classErrorMapextendsMap<string,Error>{}// no more!constErrorMap=Map<string,Error>;
Also opens up some use-cases where ReturnType didn't work well, because you can now create your own type parameter, and create a locally-concrete instantiation and fetch the return type of that function type.
declarefunctionsomeFunc<T>(x: T): {value: T};// Identical to '{ value: LocalT }'.typeFoo<LocalT>=ReturnType<typeofsomeFunc<LocalT>>;
No type arguments? (f<>)
That's always an error - you always need at least one.
Type argument list where nothing applied?
That's an error.
What if you have f<TypeArg>.prop?
Doesn't make any sense - it doesn't do anything you would possibly want. No difference between f<T>.prop and f.prop.
Nonsensical, but can you parenthesize your way out of it?
Yes.
Any parsing weirdness?
How we parse across lines.
// CallExpression with type args - differs between JS and TSf<T>(someArgs);// PR maintains JS behavior as a relational expression.f<T>someVal
Set of symbols that tell us if these type arguments can be parsed.
What about a new form of type operators that creates a deferred type application across signatures, similar to indexed access types?
Example?
functionf2<Textends<A>(x: A)=>A,Uextends<B>(y: B)=>B>(f: T|U){// what is the type of this?// T and U are generic, but currently this is notf<string>;}
When would the two be materially different?
When T or U have multiple overloads.
When T or U return an intersection for their return type.
Could do that, but we lack a type operator to do that in the type space (as opposed to the value space here).
functionf2<Textends<A>(x: A)=>A,Uextends<B>(y: B)=>B>(f: T|U){// need some operator for "applying type arguments to the signatures"// let's use faked up syntax like `%`letg: (T|U)%string=f<string>;}
Ambiguity With Arrow Function Return Types and Conditional Expressions
Instantiation Expressions
#47607
The ability to take a generic function and supply type arguments and instantiate the signatures.
You could've done this with a type assertion:
f as (some: Type, params: OtherType) => RetType
This works across both functions and constructor functions.
From the JS emit perspective, this is the same as if you'd omitted type arguments completely.
This gets rid of an unnecessary pattern people used where they'd create a subclass to specialize a constructor.
Also opens up some use-cases where
ReturnType
didn't work well, because you can now create your own type parameter, and create a locally-concrete instantiation and fetch the return type of that function type.No type arguments? (
f<>
)Type argument list where nothing applied?
What if you have
f<TypeArg>.prop
?f<T>.prop
andf.prop
.Any parsing weirdness?
How we parse across lines.
Set of symbols that tell us if these type arguments can be parsed.
What about a new form of type operators that creates a deferred type application across signatures, similar to indexed access types?
Example?
When would the two be materially different?
T
orU
have multiple overloads.T
orU
return an intersection for their return type.Could do that, but we lack a type operator to do that in the type space (as opposed to the value space here).
Ambiguity With Arrow Function Return Types and Conditional Expressions
#16241
y => ({ y })
andz => ({ z })
in a conditional expression, orz
the return type of the arrow function({ y }): z => ({ z })
The text was updated successfully, but these errors were encountered: