My expectation was that the following code would work:
// @ts-check
/**
* @template A
* @callback Identity
* @param {A} a
* @returns A
*/
/**
* @template A
* @type {Identity<A>}
*/
const id = (a) => a
/**
* @type {number}
*/
export const five = id(5); // bang!
When calling id
, I assumed that the generic type A
would be applied to type number
.
However, running tsc
results in the following errors:
error TS2322: Type 'A' is not assignable to type 'number'.
error TS2345: Argument of type 'number' is not assignable to parameter of type 'A'.
'A' could be instantiated with an arbitrary type which could be unrelated to 'number'.