Skip to content

[true, false] extends [false, false] sometimes it's true(when use infer,happen with a probability ), and sometime it's false(in most cases)??? #51090

@Max10240

Description

@Max10240

Bug Report

🔎 Search Terms

extends when infer, lazy evaluation

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________

⏯ Playground Link

Playground link with relevant code
more detail can also see: stackOverflow

💻 Code

type T0 = [IsNegative<-15>, IsNegative<90>]; // [true, false]
type T1 = Sub<-15, 90>; // ["Why?", [true, false]] /* Why [true, false] extends [false, false] ??? */

type T2 = [boolean, boolean] extends [false, false] ? true : false; // true
type T3 = [false, false] extends [boolean, boolean] ? true : false; // false

export type StartsWith<S extends string, SearchString extends string> = S extends `${SearchString}${infer T}` ? true : false;
export type IsNegative<N extends number> = StartsWith<`${N}`, '-'>;

export type Sub<A extends number, B extends number> = [IsNegative<A>, IsNegative<B>] extends infer R
    ? R extends [false, false]
        ? ['Why?', R]
        : 'Expected'
    : never;

export type Sub1<A extends number, B extends number> = [IsNegative<A>, IsNegative<B>] extends infer R
    ? [false, false] extends R
        ? ['Why?', R]
        : 'Expected'
    : never;

export type Sub2<A extends number, B extends number> = [IsNegative<A>, IsNegative<B>] extends [false, false]
    ? ['Why?',]
    : 'Expected'

export type Sub3<A extends number, B extends number> = [IsNegative<A>, IsNegative<B>][number] extends false
    ? ['Why?',]
    : 'Expected'

type T0 = [IsNegative<-15>, IsNegative<90>]; // [true, false]
type T1 = Sub<-15, 90>; // ["Why?", [true, false]] /* Why [true, false] extends [false, false] ??? */
type T2 = Sub1<-15, 90>; // Expected /* Work well */
type T3 = Sub2<-15, 90>; // ['Why?] /* Can't work too */
type T4 = Sub3<-15, 90>; // Expected /* Work well */


🙁 Actual behavior

type T1 = ["Why?", [true, false]]'

🙂 Expected behavior

type T1 = 'Expected' (Same as T2)

Metadata

Metadata

Assignees

Labels

Working as IntendedThe behavior described is the intended behavior; this is not a bug

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions