Bug Report
π Search Terms
Function Generic Type Infer String Literal Mapped Type alias
π Version & Regression Information
This is a bug in the 4.7.x version of TS.
β― Playground Link
Playground link with relevant code
π» Code
Using as in Mapped Types seems to cause the value to be incorrectly inferred in the Literal Type of the value.
declare function foo1<T extends { [P in keyof T as Capitalize<P & string>]: V }, V extends string>(a: T): T;
declare function foo2<T extends { [P in keyof T & string as Capitalize<P>]: V }, V extends string>(a: T): T;
declare function foo3<T extends { [P in keyof T]: V }, V extends string>(a: T): T;
declare function foo4<T extends { [P in Capitalize<keyof T & string>]: V }, V extends string>(a: T): T;
export const r1 = foo1({A: "a"}); // TS4.6 { A: "a"; } | TS4.7 { A: string; }
export const r2 = foo2({A: "a"}); // TS4.6 { A: "a"; } | TS4.7 any -> 'r2' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.(7022)
export const r3 = foo3({A: "a"}); // TS4.6 { A: "a"; } | TS4.7 { A: "a"; }
export const r4 = foo4({A: "a"}); // TS4.6 { A: "a"; } | TS4.7 { A: "a"; }
π Actual behavior
declare function foo1<T extends { [P in keyof T as Capitalize<P & string>]: V }, V extends string>(a: T): T;
declare function foo2<T extends { [P in keyof T & string as Capitalize<P>]: V }, V extends string>(a: T): T;
declare function foo3<T extends { [P in keyof T]: V }, V extends string>(a: T): T;
declare function foo4<T extends { [P in Capitalize<keyof T & string>]: V }, V extends string>(a: T): T;
export const r1 = foo1({A: "a"}); // β { A: string; }
export const r2 = foo2({A: "a"}); // β any -> 'r2' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.(7022)
export const r3 = foo3({A: "a"}); // β
{ A: "a"; }
export const r4 = foo4({A: "a"}); // β
{ A: "a"; }
π Expected behavior
declare function foo1<T extends { [P in keyof T as Capitalize<P & string>]: V }, V extends string>(a: T): T;
declare function foo2<T extends { [P in keyof T & string as Capitalize<P>]: V }, V extends string>(a: T): T;
declare function foo3<T extends { [P in keyof T]: V }, V extends string>(a: T): T;
declare function foo4<T extends { [P in Capitalize<keyof T & string>]: V }, V extends string>(a: T): T;
export const r1 = foo1({A: "a"}); // β
{ A: "a"; }
export const r2 = foo2({A: "a"}); // β
{ A: "a"; }
export const r3 = foo3({A: "a"}); // β
{ A: "a"; }
export const r4 = foo4({A: "a"}); // β
{ A: "a"; }
Bug Report
π Search Terms
FunctionGeneric TypeInferStringLiteralMapped Typealiasπ Version & Regression Information
This is a bug in the
4.7.xversion of TS.β― Playground Link
Playground link with relevant code
π» Code
Using
asin Mapped Types seems to cause the value to be incorrectly inferred in the Literal Type of the value.π Actual behavior
π Expected behavior