Typescript: Conditional type bug

Created on 1 Oct 2020  路  3Comments  路  Source: microsoft/TypeScript


TypeScript Version: 4.1.0-20201001


Search Terms:

Code

type A<T extends number, Q extends number> =
    [true, true] extends [IsNegative<T>, IsNegative<Q>] ? 'Every thing is ok!' : ['strange', IsNegative<T>, IsNegative<Q>]
type B = A<-2, -2>

// When using this declaration, there is bug
type IsNegative<T extends number> = `${T}` extends `-${string}` ? true : false

// When using this declaration, B is no bug
// type IsNegative<T extends number> = T extends -2 ? true : false

Expected behavior: B is "Every thing is ok!"

Actual behavior: B is ["strange", true, true]

Playground Link: https://www.typescriptlang.org/play?ts=next

Related Issues:

Bug Fix Available

Most helpful comment

The issue here is that we have no handling of wildcard types in template literal type instantiation (we use wildcard types to form "most permissive instantiations" when determining whether to defer or resolve conditional types). When any placeholder in a template literal is instantiated to a wildcard type, the entire template literal should become the wildcard type. In other words, we need to propagate wildcard types. It's a fairly simple fix, I'll put up a PR.

All 3 comments

please fill out expected behaviour and actual behaviour, make it super obvious because there are a lot of issues opened here and the team only has so much time.

Curiously this does work:

type IsNegative<T extends number> = `${T}` extends `-${string}` ? true : false
type C<Bool1, Bool2> = [true, true] extends [Bool1,Bool2] ? true : false
// Check is true
type Check = C<IsNegative<-2>, IsNegative<-2>>

@tadhgmister I have updated my code example

The issue here is that we have no handling of wildcard types in template literal type instantiation (we use wildcard types to form "most permissive instantiations" when determining whether to defer or resolve conditional types). When any placeholder in a template literal is instantiated to a wildcard type, the entire template literal should become the wildcard type. In other words, we need to propagate wildcard types. It's a fairly simple fix, I'll put up a PR.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

manekinekko picture manekinekko  路  3Comments

dlaberge picture dlaberge  路  3Comments

kyasbal-1994 picture kyasbal-1994  路  3Comments

weswigham picture weswigham  路  3Comments

uber5001 picture uber5001  路  3Comments