You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
{[K in T]: (arg: K) => void}[T] has inconsistent/unsound behavior when T is a lazy-evaluated unionΒ #48734
// Literal versiontypeU1={foo: (arg: "foo")=>void;bar: (arg: "bar")=>void;}// Mapped type versiontypeU2={[Kin'foo'|'bar']: (arg: K)=>void}// ^? type U2 = { foo: (arg: "foo") => void; bar: (arg: "bar") => void; }typeIs_U2_equivalent_to_U1=U1extendsU2 ? U2extendsU1 ? true : false : false;// ^? type Is_U2_equivalent_to_U1 = truefunctionfn1<Textends'foo'|'bar'>(cb: {[KinT]: U1[K]}[T],arg: T): void{cb(arg);// correct error}functionfn2<Textends'foo'|'bar'>(cb: {[KinT]: U2[K]}[T],arg: T): void{cb(arg);// // correct error before 4.6, compiles after 4.6}functionfn3<Textends'foo'|'bar'>(cb: {[KinT]: (arg: K)=>void}[T],arg: T): void{cb(arg);// should error but compiles}functionfn4<Textends'foo'|'bar'>(cb: Textendsunknown ? (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')
Activity
RyanCavanaugh commentedon Apr 18, 2022
See #48730
typescript-bot commentedon Apr 21, 2022
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.