There have been a lot of users who find it strange that this would compile:
declarefunctionforEach<U>(array: U[],callback: (item: U)=>void);varstrings: string[];varcallback: (item: {})=>void;forEach(strings,callback);// U is inferred as {}Suggestion: use the decorator @infer to tell the compiler to infer a generic from that argument.
Example:
declarefunctionforEach<U>(@infer<U>array: U[],callback: (item: U)=>void);varstrings: string[];varcallback: (item: {})=>void;forEach(strings,callback);// U is inferred as string (using only the decorated argument), thus this is an errorYou can also add @infer to multiple arguments, like this:
declarefunctionforEachOnTwoArrays<U>(@infer<U>firstArray: U[], @infer<U>secondArray: U[],callback: (item: U)=>void);
Adding @infer to all parameters would have the same behavior as having no @infer. Adding @infer is optional, thus not a breaking change.
Rules for @infer:
- You may only add type parameters of the direct containing function.
- You can add multiple type parameters like this:
@infer<T> @infer<U> (or maybe @infer<T, U>?) - Generics that are not referenced from an
@infer decorator are inferred using the current behavior - Every generic
T that is referenced from an @infer<T> decorator is resolved using all parameters that are decorated with @infer<T> of that function.
Note that this syntax is currently not supported, as the compiler expects ( after the generics. An alternative would be to write it as @infer<U>(), but I think @infer<U> is cleaner.
There have been a lot of users who find it strange that this would compile:
Suggestion: use the decorator
@inferto tell the compiler to infer a generic from that argument.Example:
You can also add
@inferto multiple arguments, like this:Adding
@inferto all parameters would have the same behavior as having no@infer. Adding@inferis optional, thus not a breaking change.Rules for
@infer:@infer<T> @infer<U>(or maybe@infer<T, U>?)@inferdecorator are inferred using the current behaviorTthat is referenced from an@infer<T>decorator is resolved using all parameters that are decorated with@infer<T>of that function.Note that this syntax is currently not supported, as the compiler expects
(after the generics. An alternative would be to write it as@infer<U>(), but I think@infer<U>is cleaner.