ts-toolbelt icon indicating copy to clipboard operation
ts-toolbelt copied to clipboard

`Narrow` doesn't work on template literal types

Open DetachHead opened this issue 4 years ago • 1 comments

🐞 Bug Report

Reproduce the bug

declare const foo: <T extends number>(value: Narrow<`${T}`>) => void

foo('12')

Screenshots

image

Additional context

trying to make a toNumber function that knows the value at compiletime

const toNumber = <T extends number>(value: Narrow<`${T}`>): T => Number(value) as T

const value: 12 = toNumber('12') //TS2322: Type 'number' is not assignable to type '12'.

DetachHead avatar Oct 15 '21 03:10 DetachHead

may be a limitation in the compiler related to https://github.com/microsoft/TypeScript/issues/42938

DetachHead avatar Oct 15 '21 03:10 DetachHead