Skip to content

{[K in T]: (arg: K) => void}[T] has inconsistent/unsound behavior when T is a lazy-evaluated union #48734

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
whzx5byb opened this issue Apr 17, 2022 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@whzx5byb
Copy link

whzx5byb commented Apr 17, 2022

Bug Report

πŸ”Ž Search Terms

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and some behavior changed between versions 4.5.5 and 4.6.2

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

// Literal version
type U1 = {
    foo: (arg: "foo") => void;
    bar: (arg: "bar") => void;
}

// Mapped type version
type U2 = { [K in 'foo' | 'bar']: (arg: K) => void }
//   ^? type U2 = { foo: (arg: "foo") => void; bar: (arg: "bar") => void; }

type Is_U2_equivalent_to_U1 = U1 extends U2 ? U2 extends U1 ? true : false : false;
//   ^? type Is_U2_equivalent_to_U1 = true

function fn1<T extends 'foo' | 'bar'>(cb: {[K in T]: U1[K]}[T], arg: T): void {
    cb(arg); // correct error
}

function fn2<T extends 'foo' | 'bar'>(cb: {[K in T]: U2[K]}[T], arg: T): void {
    cb(arg); // // correct error before 4.6, compiles after 4.6
}

function fn3<T extends 'foo' | 'bar'>(cb: {[K in T]: (arg: K) => void}[T], arg: T): void {
    cb(arg); // should error but compiles
}

function fn4<T extends 'foo' | 'bar'>(cb: T extends unknown ? (arg: T) => void : never, arg: T): void {
    cb(arg); // correct error
}

πŸ™ Actual behavior

In 4.5.5, fn3 doesn't report an error.
In 4.6.2, fn2 and fn3 doesn't report an error.
I'm not sure but this maybe cause by #47109 or #47370 ?

πŸ™‚ Expected behavior

Error reported in fn1 and fn4 should also be reported in fn2 and fn3, because:

  • Provided that U1 is equivalent to U2, it is inconsistent where fn1 reports error but fn2 does not.
  • The pattern {[K in T]: G<K>}[T] has a distributive behavior over T, so I expect it behave the same as the conditional type form T extends unknown ? G<T> : never, which reports error correctly as fn4 shows.
  • An unsound call for fn3:
    fn3<'foo' | 'bar'>((x: 'foo') => assert(x === 'foo'), 'bar')
@RyanCavanaugh
Copy link
Member

See #48730

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Apr 18, 2022
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants