Skip to content

Assignability rule for conditional types needs to require check types and distributivity to be identical #27118

Open
@mattmccutchen

Description

@mattmccutchen

TypeScript Version: master (e471856)

Search Terms: assignable assignability conditional type check type checkType distributive distributivity identical unsound

Code

type MyElement<A> = [A] extends [[infer E]] ? E : never;
function oops<A, B extends A>(arg: MyElement<A>): MyElement<B> { 
    return arg;  // compiles OK, expected compile error
}
oops<[number | string], [string]>(42).slice();  // runtime error

type MyAcceptor<A> = [A] extends [[infer E]] ? (arg: E) => void : never;
function oops2<A, B extends A>(arg: MyAcceptor<B>): MyAcceptor<A> { 
    return arg;  // compiles OK, expected compile error
}
oops2<[number | string], [string]>((arg) => arg.slice())(42);  // runtime error

type Dist<T> = T extends number ? number : string; 
type Aux<A extends { a: unknown }> = A["a"] extends number ? number : string;
type Nondist<T> = Aux<{a: T}>;
function oops3<T>(arg: Dist<T>): Nondist<T> {
    return arg;  // compiles OK, expected compile error
}
oops3<number | string>(42).slice();  // runtime error

Expected behavior: Compile errors as marked.

Actual behavior: Compiles OK, runtime errors as marked.

Playground Link: link

Related Issues: None found

Metadata

Metadata

Assignees

No one assigned

    Labels

    Experimentation NeededSomeone needs to try this out to see what happensSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions