TypeScriptTips

Make bugs impossible.
One TypeScript tip at a time.

I like the tricks :) – Kasia

WTH?!

From time to time, I get stuck into the following error. With this tip, I'm making sure the next time I google for it, I'll see my own explanation.

const identityBool = <T extends boolean>(x: T = false): T => x // ⛔️
// Type 'boolean' is not assignable to type 'T'.
//   'boolean' is assignable to the constraint of type 'T',
//    but 'T' could be instantiated with a different subtype
//    of constraint 'boolean'.

const identityTrue: <T extends true>(x: T) => T = identityBool
// This is what the error above refers to.