From 7a62ad266f1bd56ae25ee6f1d7fd45348cd4bd19 Mon Sep 17 00:00:00 2001 From: zhengdaodeguL <1159957339@qq.com> Date: Tue, 17 Nov 2026 07:51:52 +0000 Subject: [PATCH] Fix contextual typing loss for computed string keys under circular mapped-type key constraints When a mapped type's key constraint resolves to the error type because its base constraint is circular, resolveMappedTypeMembers treated it as an any key and synthesized a spurious [x: string]: undefined index signature. That signature merged with the contextual type of computed string keys, producing ((el: number) => unknown) & undefined and leaving callback parameters implicitly any (TS7006). Skip member creation when the key constraint is the error type with a circular base constraint, so the mapped type contributes no members; genuine string/any keys and unresolved-name cases are unchanged. Fixes #65530 --- tsc/internal/checker/checker.go | 5 + ...WithCircularMappedKeyConstraint.errors.txt | 106 ++++++++ ...ypeWithCircularMappedKeyConstraint.symbols | 232 ++++++++++++++++++ ...lTypeWithCircularMappedKeyConstraint.types | 215 ++++++++++++++++ ...tualTypeWithCircularMappedKeyConstraint.ts | 55 +++++ 5 files changed, 613 insertions(+) create mode 100644 tsc/testdata/baselines/reference/compiler/contextualTypeWithCircularMappedKeyConstraint.errors.txt create mode 100644 tsc/testdata/baselines/reference/compiler/contextualTypeWithCircularMappedKeyConstraint.symbols create mode 100644 tsc/testdata/baselines/reference/compiler/contextualTypeWithCircularMappedKeyConstraint.types create mode 100644 tsc/testdata/tests/cases/compiler/contextualTypeWithCircularMappedKeyConstraint.ts diff --git a/tsc/internal/checker/checker.go b/tsc/internal/checker/checker.go index 14f8356152d83..73e1479c45f4b 100644 --- a/tsc/internal/checker/checker.go +++ b/tsc/internal/checker/checker.go @@ -21579,6 +21579,11 @@ func (c *Checker) resolveMappedTypeMembers(t *Type) { // and T as the template type. typeParameter := c.getTypeParameterFromMappedType(t) constraintType := c.getConstraintTypeFromMappedType(t) + if constraintType == c.errorType && !c.hasNonCircularBaseConstraint(typeParameter) { + // A circular key constraint falls back to the error type. Don't treat it as + // an any key and synthesize an index signature from it. + return + } mappedType := core.OrElse(t.AsMappedType().target, t) nameType := c.getNameTypeFromMappedType(mappedType) shouldLinkPropDeclarations := c.getMappedTypeNameTypeKind(mappedType) != MappedTypeNameTypeKindRemapping diff --git a/tsc/testdata/baselines/reference/compiler/contextualTypeWithCircularMappedKeyConstraint.errors.txt b/tsc/testdata/baselines/reference/compiler/contextualTypeWithCircularMappedKeyConstraint.errors.txt new file mode 100644 index 0000000000000..fa0ff32b75512 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/contextualTypeWithCircularMappedKeyConstraint.errors.txt @@ -0,0 +1,106 @@ +main.ts(17,24): error TS2322: Type '{ [x: string]: (el: any) => any; }' is not assignable to type 'Rule & { [x: string]: undefined; }'. + Type '{ [x: string]: (el: any) => any; }' is not assignable to type '{ [x: string]: undefined; }'. + 'string' index signatures are incompatible. + Type '(el: any) => any' is not assignable to type 'undefined'. +main.ts(17,35): error TS7006: Parameter 'el' implicitly has an 'any' type. +main.ts(18,30): error TS2322: Type '{ [x: string]: (el: any) => any; }' is not assignable to type 'Rule & { [x: string]: undefined; }'. + Type '{ [x: string]: (el: any) => any; }' is not assignable to type '{ [x: string]: undefined; }'. + 'string' index signatures are incompatible. + Type '(el: any) => any' is not assignable to type 'undefined'. +main.ts(18,41): error TS7006: Parameter 'el' implicitly has an 'any' type. +main.ts(19,29): error TS2322: Type '{ [x: string]: (el: any) => any; }' is not assignable to type 'Rule & { [x: string]: undefined; [x: number]: undefined; [x: symbol]: undefined; }'. + Type '{ [x: string]: (el: any) => any; }' is not assignable to type '{ [x: string]: undefined; [x: number]: undefined; [x: symbol]: undefined; }'. + 'string' index signatures are incompatible. + Type '(el: any) => any' is not assignable to type 'undefined'. +main.ts(19,40): error TS7006: Parameter 'el' implicitly has an 'any' type. +main.ts(23,36): error TS2322: Type '{ [x: string]: (el: any) => any; }' is not assignable to type 'Rule & { [x: string]: undefined; }'. + Type '{ [x: string]: (el: any) => any; }' is not assignable to type '{ [x: string]: undefined; }'. + 'string' index signatures are incompatible. + Type '(el: any) => any' is not assignable to type 'undefined'. +main.ts(23,47): error TS7006: Parameter 'el' implicitly has an 'any' type. + + +==== library.d.ts (0 errors) ==== + type NoStrayRuleKeys = { [K in keyof R]: unknown }; + type StrictRules = ([keyof R] extends [never] ? Required : NoStrayRuleKeys) & { + [K in R extends unknown ? Exclude : never]?: never; + }; + export declare class Deriver { + derive>(build: () => R): void; + plain(build: () => R): void; + } + + export declare class RealIndex { + derive(build: () => R): void; + } + export declare class RealMappedIndex { + derive(build: () => R): void; + } + export declare class AnyMappedIndex { + derive(build: () => R): void; + } + export declare class UnresolvedMappedIndex { + derive(build: () => R): void; + } + +==== main.ts (8 errors) ==== + import type { Deriver, RealIndex, RealMappedIndex, AnyMappedIndex, UnresolvedMappedIndex } from "./library.js"; + + declare const key: string; + type Rule = { [x: string]: (el: number) => unknown }; + declare const indexed: Deriver; + declare const literal: Deriver<{ rows?: (el: number) => unknown }>; + + // Repro from #65530: a failed circular key constraint must not add an index signature. + indexed.derive(() => ({ [key]: (el) => el.toFixed() })); + literal.derive(() => ({ rows: (el) => el.toFixed() })); + indexed.plain(() => ({ [key]: (el) => el.toFixed() })); + + // Genuine index signatures still contribute to the contextual type, including any keys. + declare const realIndex: RealIndex; + declare const realMappedIndex: RealMappedIndex; + declare const anyMappedIndex: AnyMappedIndex; + realIndex.derive(() => ({ [key]: (el) => el })); + ~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2322: Type '{ [x: string]: (el: any) => any; }' is not assignable to type 'Rule & { [x: string]: undefined; }'. +!!! error TS2322: Type '{ [x: string]: (el: any) => any; }' is not assignable to type '{ [x: string]: undefined; }'. +!!! error TS2322: 'string' index signatures are incompatible. +!!! error TS2322: Type '(el: any) => any' is not assignable to type 'undefined'. +!!! related TS6502 library.d.ts:11:61: The expected type comes from the return type of this signature. + ~~ +!!! error TS7006: Parameter 'el' implicitly has an 'any' type. + realMappedIndex.derive(() => ({ [key]: (el) => el })); + ~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2322: Type '{ [x: string]: (el: any) => any; }' is not assignable to type 'Rule & { [x: string]: undefined; }'. +!!! error TS2322: Type '{ [x: string]: (el: any) => any; }' is not assignable to type '{ [x: string]: undefined; }'. +!!! error TS2322: 'string' index signatures are incompatible. +!!! error TS2322: Type '(el: any) => any' is not assignable to type 'undefined'. +!!! related TS6502 library.d.ts:14:60: The expected type comes from the return type of this signature. + ~~ +!!! error TS7006: Parameter 'el' implicitly has an 'any' type. + anyMappedIndex.derive(() => ({ [key]: (el) => el })); + ~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2322: Type '{ [x: string]: (el: any) => any; }' is not assignable to type 'Rule & { [x: string]: undefined; [x: number]: undefined; [x: symbol]: undefined; }'. +!!! error TS2322: Type '{ [x: string]: (el: any) => any; }' is not assignable to type '{ [x: string]: undefined; [x: number]: undefined; [x: symbol]: undefined; }'. +!!! error TS2322: 'string' index signatures are incompatible. +!!! error TS2322: Type '(el: any) => any' is not assignable to type 'undefined'. +!!! related TS6502 library.d.ts:17:57: The expected type comes from the return type of this signature. + ~~ +!!! error TS7006: Parameter 'el' implicitly has an 'any' type. + + // An unresolved name is an error key type, but its constraint is not circular. + declare const unresolvedMappedIndex: UnresolvedMappedIndex; + unresolvedMappedIndex.derive(() => ({ [key]: (el) => el })); + ~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2322: Type '{ [x: string]: (el: any) => any; }' is not assignable to type 'Rule & { [x: string]: undefined; }'. +!!! error TS2322: Type '{ [x: string]: (el: any) => any; }' is not assignable to type '{ [x: string]: undefined; }'. +!!! error TS2322: 'string' index signatures are incompatible. +!!! error TS2322: Type '(el: any) => any' is not assignable to type 'undefined'. +!!! related TS6502 library.d.ts:20:65: The expected type comes from the return type of this signature. + ~~ +!!! error TS7006: Parameter 'el' implicitly has an 'any' type. + + const anyKeyedCallbacks: { [K in any]: (el: number) => unknown } = { + [key]: (el) => el.toFixed(), + }; + \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/contextualTypeWithCircularMappedKeyConstraint.symbols b/tsc/testdata/baselines/reference/compiler/contextualTypeWithCircularMappedKeyConstraint.symbols new file mode 100644 index 0000000000000..dfff0d7bdcae2 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/contextualTypeWithCircularMappedKeyConstraint.symbols @@ -0,0 +1,232 @@ +//// [tests/cases/compiler/contextualTypeWithCircularMappedKeyConstraint.ts] //// + +=== library.d.ts === +type NoStrayRuleKeys = { [K in keyof R]: unknown }; +>NoStrayRuleKeys : Symbol(NoStrayRuleKeys, Decl(library.d.ts, 0, 0)) +>R : Symbol(R, Decl(library.d.ts, 0, 21)) +>K : Symbol(K, Decl(library.d.ts, 0, 29)) +>R : Symbol(R, Decl(library.d.ts, 0, 21)) + +type StrictRules = ([keyof R] extends [never] ? Required : NoStrayRuleKeys) & { +>StrictRules : Symbol(StrictRules, Decl(library.d.ts, 0, 54)) +>R : Symbol(R, Decl(library.d.ts, 1, 17)) +>T : Symbol(T, Decl(library.d.ts, 1, 19)) +>R : Symbol(R, Decl(library.d.ts, 1, 17)) +>Required : Symbol(Required, Decl(lib.es5.d.ts, --, --)) +>T : Symbol(T, Decl(library.d.ts, 1, 19)) +>NoStrayRuleKeys : Symbol(NoStrayRuleKeys, Decl(library.d.ts, 0, 0)) +>R : Symbol(R, Decl(library.d.ts, 1, 17)) + + [K in R extends unknown ? Exclude : never]?: never; +>K : Symbol(K, Decl(library.d.ts, 2, 5)) +>R : Symbol(R, Decl(library.d.ts, 1, 17)) +>Exclude : Symbol(Exclude, Decl(lib.es5.d.ts, --, --)) +>R : Symbol(R, Decl(library.d.ts, 1, 17)) +>T : Symbol(T, Decl(library.d.ts, 1, 19)) + +}; +export declare class Deriver { +>Deriver : Symbol(Deriver, Decl(library.d.ts, 3, 2)) +>T : Symbol(T, Decl(library.d.ts, 4, 29)) + + derive>(build: () => R): void; +>derive : Symbol(Deriver.derive, Decl(library.d.ts, 4, 33)) +>R : Symbol(R, Decl(library.d.ts, 5, 11)) +>T : Symbol(T, Decl(library.d.ts, 4, 29)) +>StrictRules : Symbol(StrictRules, Decl(library.d.ts, 0, 54)) +>R : Symbol(R, Decl(library.d.ts, 5, 11)) +>T : Symbol(T, Decl(library.d.ts, 4, 29)) +>build : Symbol(build, Decl(library.d.ts, 5, 44)) +>R : Symbol(R, Decl(library.d.ts, 5, 11)) + + plain(build: () => R): void; +>plain : Symbol(Deriver.plain, Decl(library.d.ts, 5, 66)) +>R : Symbol(R, Decl(library.d.ts, 6, 10)) +>T : Symbol(T, Decl(library.d.ts, 4, 29)) +>build : Symbol(build, Decl(library.d.ts, 6, 23)) +>R : Symbol(R, Decl(library.d.ts, 6, 10)) +} + +export declare class RealIndex { +>RealIndex : Symbol(RealIndex, Decl(library.d.ts, 7, 1)) +>T : Symbol(T, Decl(library.d.ts, 9, 31)) + + derive(build: () => R): void; +>derive : Symbol(RealIndex.derive, Decl(library.d.ts, 9, 35)) +>R : Symbol(R, Decl(library.d.ts, 10, 11)) +>T : Symbol(T, Decl(library.d.ts, 9, 31)) +>x : Symbol(x, Decl(library.d.ts, 10, 28)) +>build : Symbol(build, Decl(library.d.ts, 10, 53)) +>R : Symbol(R, Decl(library.d.ts, 10, 11)) +} +export declare class RealMappedIndex { +>RealMappedIndex : Symbol(RealMappedIndex, Decl(library.d.ts, 11, 1)) +>T : Symbol(T, Decl(library.d.ts, 12, 37)) + + derive(build: () => R): void; +>derive : Symbol(RealMappedIndex.derive, Decl(library.d.ts, 12, 41)) +>R : Symbol(R, Decl(library.d.ts, 13, 11)) +>T : Symbol(T, Decl(library.d.ts, 12, 37)) +>K : Symbol(K, Decl(library.d.ts, 13, 28)) +>build : Symbol(build, Decl(library.d.ts, 13, 52)) +>R : Symbol(R, Decl(library.d.ts, 13, 11)) +} +export declare class AnyMappedIndex { +>AnyMappedIndex : Symbol(AnyMappedIndex, Decl(library.d.ts, 14, 1)) +>T : Symbol(T, Decl(library.d.ts, 15, 36)) + + derive(build: () => R): void; +>derive : Symbol(AnyMappedIndex.derive, Decl(library.d.ts, 15, 40)) +>R : Symbol(R, Decl(library.d.ts, 16, 11)) +>T : Symbol(T, Decl(library.d.ts, 15, 36)) +>K : Symbol(K, Decl(library.d.ts, 16, 28)) +>build : Symbol(build, Decl(library.d.ts, 16, 49)) +>R : Symbol(R, Decl(library.d.ts, 16, 11)) +} +export declare class UnresolvedMappedIndex { +>UnresolvedMappedIndex : Symbol(UnresolvedMappedIndex, Decl(library.d.ts, 17, 1)) +>T : Symbol(T, Decl(library.d.ts, 18, 43)) + + derive(build: () => R): void; +>derive : Symbol(UnresolvedMappedIndex.derive, Decl(library.d.ts, 18, 47)) +>R : Symbol(R, Decl(library.d.ts, 19, 11)) +>T : Symbol(T, Decl(library.d.ts, 18, 43)) +>K : Symbol(K, Decl(library.d.ts, 19, 28)) +>MissingKeys : Symbol(MissingKeys) +>build : Symbol(build, Decl(library.d.ts, 19, 57)) +>R : Symbol(R, Decl(library.d.ts, 19, 11)) +} + +=== main.ts === +import type { Deriver, RealIndex, RealMappedIndex, AnyMappedIndex, UnresolvedMappedIndex } from "./library.js"; +>Deriver : Symbol(Deriver, Decl(main.ts, 0, 13)) +>RealIndex : Symbol(RealIndex, Decl(main.ts, 0, 22)) +>RealMappedIndex : Symbol(RealMappedIndex, Decl(main.ts, 0, 33)) +>AnyMappedIndex : Symbol(AnyMappedIndex, Decl(main.ts, 0, 50)) +>UnresolvedMappedIndex : Symbol(UnresolvedMappedIndex, Decl(main.ts, 0, 66)) + +declare const key: string; +>key : Symbol(key, Decl(main.ts, 2, 13)) + +type Rule = { [x: string]: (el: number) => unknown }; +>Rule : Symbol(Rule, Decl(main.ts, 2, 26)) +>x : Symbol(x, Decl(main.ts, 3, 15)) +>el : Symbol(el, Decl(main.ts, 3, 28)) + +declare const indexed: Deriver; +>indexed : Symbol(indexed, Decl(main.ts, 4, 13)) +>Deriver : Symbol(Deriver, Decl(main.ts, 0, 13)) +>Rule : Symbol(Rule, Decl(main.ts, 2, 26)) + +declare const literal: Deriver<{ rows?: (el: number) => unknown }>; +>literal : Symbol(literal, Decl(main.ts, 5, 13)) +>Deriver : Symbol(Deriver, Decl(main.ts, 0, 13)) +>rows : Symbol(rows, Decl(main.ts, 5, 32)) +>el : Symbol(el, Decl(main.ts, 5, 41)) + +// Repro from #65530: a failed circular key constraint must not add an index signature. +indexed.derive(() => ({ [key]: (el) => el.toFixed() })); +>indexed.derive : Symbol(Deriver.derive, Decl(library.d.ts, 4, 33)) +>indexed : Symbol(indexed, Decl(main.ts, 4, 13)) +>derive : Symbol(Deriver.derive, Decl(library.d.ts, 4, 33)) +>[key] : Symbol([key], Decl(main.ts, 8, 23)) +>key : Symbol(key, Decl(main.ts, 2, 13)) +>el : Symbol(el, Decl(main.ts, 8, 32)) +>el.toFixed : Symbol(Number.toFixed, Decl(lib.es5.d.ts, --, --)) +>el : Symbol(el, Decl(main.ts, 8, 32)) +>toFixed : Symbol(Number.toFixed, Decl(lib.es5.d.ts, --, --)) + +literal.derive(() => ({ rows: (el) => el.toFixed() })); +>literal.derive : Symbol(Deriver.derive, Decl(library.d.ts, 4, 33)) +>literal : Symbol(literal, Decl(main.ts, 5, 13)) +>derive : Symbol(Deriver.derive, Decl(library.d.ts, 4, 33)) +>rows : Symbol(rows, Decl(main.ts, 9, 23)) +>el : Symbol(el, Decl(main.ts, 9, 31)) +>el.toFixed : Symbol(Number.toFixed, Decl(lib.es5.d.ts, --, --)) +>el : Symbol(el, Decl(main.ts, 9, 31)) +>toFixed : Symbol(Number.toFixed, Decl(lib.es5.d.ts, --, --)) + +indexed.plain(() => ({ [key]: (el) => el.toFixed() })); +>indexed.plain : Symbol(Deriver.plain, Decl(library.d.ts, 5, 66)) +>indexed : Symbol(indexed, Decl(main.ts, 4, 13)) +>plain : Symbol(Deriver.plain, Decl(library.d.ts, 5, 66)) +>[key] : Symbol([key], Decl(main.ts, 10, 22)) +>key : Symbol(key, Decl(main.ts, 2, 13)) +>el : Symbol(el, Decl(main.ts, 10, 31)) +>el.toFixed : Symbol(Number.toFixed, Decl(lib.es5.d.ts, --, --)) +>el : Symbol(el, Decl(main.ts, 10, 31)) +>toFixed : Symbol(Number.toFixed, Decl(lib.es5.d.ts, --, --)) + +// Genuine index signatures still contribute to the contextual type, including any keys. +declare const realIndex: RealIndex; +>realIndex : Symbol(realIndex, Decl(main.ts, 13, 13)) +>RealIndex : Symbol(RealIndex, Decl(main.ts, 0, 22)) +>Rule : Symbol(Rule, Decl(main.ts, 2, 26)) + +declare const realMappedIndex: RealMappedIndex; +>realMappedIndex : Symbol(realMappedIndex, Decl(main.ts, 14, 13)) +>RealMappedIndex : Symbol(RealMappedIndex, Decl(main.ts, 0, 33)) +>Rule : Symbol(Rule, Decl(main.ts, 2, 26)) + +declare const anyMappedIndex: AnyMappedIndex; +>anyMappedIndex : Symbol(anyMappedIndex, Decl(main.ts, 15, 13)) +>AnyMappedIndex : Symbol(AnyMappedIndex, Decl(main.ts, 0, 50)) +>Rule : Symbol(Rule, Decl(main.ts, 2, 26)) + +realIndex.derive(() => ({ [key]: (el) => el })); +>realIndex.derive : Symbol(RealIndex.derive, Decl(library.d.ts, 9, 35)) +>realIndex : Symbol(realIndex, Decl(main.ts, 13, 13)) +>derive : Symbol(RealIndex.derive, Decl(library.d.ts, 9, 35)) +>[key] : Symbol([key], Decl(main.ts, 16, 25)) +>key : Symbol(key, Decl(main.ts, 2, 13)) +>el : Symbol(el, Decl(main.ts, 16, 34)) +>el : Symbol(el, Decl(main.ts, 16, 34)) + +realMappedIndex.derive(() => ({ [key]: (el) => el })); +>realMappedIndex.derive : Symbol(RealMappedIndex.derive, Decl(library.d.ts, 12, 41)) +>realMappedIndex : Symbol(realMappedIndex, Decl(main.ts, 14, 13)) +>derive : Symbol(RealMappedIndex.derive, Decl(library.d.ts, 12, 41)) +>[key] : Symbol([key], Decl(main.ts, 17, 31)) +>key : Symbol(key, Decl(main.ts, 2, 13)) +>el : Symbol(el, Decl(main.ts, 17, 40)) +>el : Symbol(el, Decl(main.ts, 17, 40)) + +anyMappedIndex.derive(() => ({ [key]: (el) => el })); +>anyMappedIndex.derive : Symbol(AnyMappedIndex.derive, Decl(library.d.ts, 15, 40)) +>anyMappedIndex : Symbol(anyMappedIndex, Decl(main.ts, 15, 13)) +>derive : Symbol(AnyMappedIndex.derive, Decl(library.d.ts, 15, 40)) +>[key] : Symbol([key], Decl(main.ts, 18, 30)) +>key : Symbol(key, Decl(main.ts, 2, 13)) +>el : Symbol(el, Decl(main.ts, 18, 39)) +>el : Symbol(el, Decl(main.ts, 18, 39)) + +// An unresolved name is an error key type, but its constraint is not circular. +declare const unresolvedMappedIndex: UnresolvedMappedIndex; +>unresolvedMappedIndex : Symbol(unresolvedMappedIndex, Decl(main.ts, 21, 13)) +>UnresolvedMappedIndex : Symbol(UnresolvedMappedIndex, Decl(main.ts, 0, 66)) +>Rule : Symbol(Rule, Decl(main.ts, 2, 26)) + +unresolvedMappedIndex.derive(() => ({ [key]: (el) => el })); +>unresolvedMappedIndex.derive : Symbol(UnresolvedMappedIndex.derive, Decl(library.d.ts, 18, 47)) +>unresolvedMappedIndex : Symbol(unresolvedMappedIndex, Decl(main.ts, 21, 13)) +>derive : Symbol(UnresolvedMappedIndex.derive, Decl(library.d.ts, 18, 47)) +>[key] : Symbol([key], Decl(main.ts, 22, 37)) +>key : Symbol(key, Decl(main.ts, 2, 13)) +>el : Symbol(el, Decl(main.ts, 22, 46)) +>el : Symbol(el, Decl(main.ts, 22, 46)) + +const anyKeyedCallbacks: { [K in any]: (el: number) => unknown } = { +>anyKeyedCallbacks : Symbol(anyKeyedCallbacks, Decl(main.ts, 24, 5)) +>K : Symbol(K, Decl(main.ts, 24, 28)) +>el : Symbol(el, Decl(main.ts, 24, 40)) + + [key]: (el) => el.toFixed(), +>[key] : Symbol([key], Decl(main.ts, 24, 68)) +>key : Symbol(key, Decl(main.ts, 2, 13)) +>el : Symbol(el, Decl(main.ts, 25, 12)) +>el.toFixed : Symbol(Number.toFixed, Decl(lib.es5.d.ts, --, --)) +>el : Symbol(el, Decl(main.ts, 25, 12)) +>toFixed : Symbol(Number.toFixed, Decl(lib.es5.d.ts, --, --)) + +}; + diff --git a/tsc/testdata/baselines/reference/compiler/contextualTypeWithCircularMappedKeyConstraint.types b/tsc/testdata/baselines/reference/compiler/contextualTypeWithCircularMappedKeyConstraint.types new file mode 100644 index 0000000000000..907963a128290 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/contextualTypeWithCircularMappedKeyConstraint.types @@ -0,0 +1,215 @@ +//// [tests/cases/compiler/contextualTypeWithCircularMappedKeyConstraint.ts] //// + +=== library.d.ts === +type NoStrayRuleKeys = { [K in keyof R]: unknown }; +>NoStrayRuleKeys : NoStrayRuleKeys + +type StrictRules = ([keyof R] extends [never] ? Required : NoStrayRuleKeys) & { +>StrictRules : StrictRules + + [K in R extends unknown ? Exclude : never]?: never; +}; +export declare class Deriver { +>Deriver : Deriver + + derive>(build: () => R): void; +>derive : >(build: () => R) => void +>build : () => R + + plain(build: () => R): void; +>plain : (build: () => R) => void +>build : () => R +} + +export declare class RealIndex { +>RealIndex : RealIndex + + derive(build: () => R): void; +>derive : (build: () => R) => void +>x : string +>build : () => R +} +export declare class RealMappedIndex { +>RealMappedIndex : RealMappedIndex + + derive(build: () => R): void; +>derive : (build: () => R) => void +>build : () => R +} +export declare class AnyMappedIndex { +>AnyMappedIndex : AnyMappedIndex + + derive(build: () => R): void; +>derive : (build: () => R) => void +>build : () => R +} +export declare class UnresolvedMappedIndex { +>UnresolvedMappedIndex : UnresolvedMappedIndex + + derive(build: () => R): void; +>derive : (build: () => R) => void +>build : () => R +} + +=== main.ts === +import type { Deriver, RealIndex, RealMappedIndex, AnyMappedIndex, UnresolvedMappedIndex } from "./library.js"; +>Deriver : Deriver +>RealIndex : RealIndex +>RealMappedIndex : RealMappedIndex +>AnyMappedIndex : AnyMappedIndex +>UnresolvedMappedIndex : UnresolvedMappedIndex + +declare const key: string; +>key : string + +type Rule = { [x: string]: (el: number) => unknown }; +>Rule : Rule +>x : string +>el : number + +declare const indexed: Deriver; +>indexed : Deriver + +declare const literal: Deriver<{ rows?: (el: number) => unknown }>; +>literal : Deriver<{ rows?: (el: number) => unknown; }> +>rows : ((el: number) => unknown) | undefined +>el : number + +// Repro from #65530: a failed circular key constraint must not add an index signature. +indexed.derive(() => ({ [key]: (el) => el.toFixed() })); +>indexed.derive(() => ({ [key]: (el) => el.toFixed() })) : void +>indexed.derive : >(build: () => R) => void +>indexed : Deriver +>derive : >(build: () => R) => void +>() => ({ [key]: (el) => el.toFixed() }) : () => { [key]: (el: number) => string; } +>({ [key]: (el) => el.toFixed() }) : { [key]: (el: number) => string; } +>{ [key]: (el) => el.toFixed() } : { [key]: (el: number) => string; } +>[key] : (el: number) => string +>key : string +>(el) => el.toFixed() : (el: number) => string +>el : number +>el.toFixed() : string +>el.toFixed : (fractionDigits?: number) => string +>el : number +>toFixed : (fractionDigits?: number) => string + +literal.derive(() => ({ rows: (el) => el.toFixed() })); +>literal.derive(() => ({ rows: (el) => el.toFixed() })) : void +>literal.derive : unknown; } & import("./library.js").StrictRules unknown; }>>(build: () => R) => void +>literal : Deriver<{ rows?: (el: number) => unknown; }> +>derive : unknown; } & import("./library.js").StrictRules unknown; }>>(build: () => R) => void +>() => ({ rows: (el) => el.toFixed() }) : () => { rows: (el: number) => string; } +>({ rows: (el) => el.toFixed() }) : { rows: (el: number) => string; } +>{ rows: (el) => el.toFixed() } : { rows: (el: number) => string; } +>rows : (el: number) => string +>(el) => el.toFixed() : (el: number) => string +>el : number +>el.toFixed() : string +>el.toFixed : (fractionDigits?: number) => string +>el : number +>toFixed : (fractionDigits?: number) => string + +indexed.plain(() => ({ [key]: (el) => el.toFixed() })); +>indexed.plain(() => ({ [key]: (el) => el.toFixed() })) : void +>indexed.plain : (build: () => R) => void +>indexed : Deriver +>plain : (build: () => R) => void +>() => ({ [key]: (el) => el.toFixed() }) : () => { [key]: (el: number) => string; } +>({ [key]: (el) => el.toFixed() }) : { [key]: (el: number) => string; } +>{ [key]: (el) => el.toFixed() } : { [key]: (el: number) => string; } +>[key] : (el: number) => string +>key : string +>(el) => el.toFixed() : (el: number) => string +>el : number +>el.toFixed() : string +>el.toFixed : (fractionDigits?: number) => string +>el : number +>toFixed : (fractionDigits?: number) => string + +// Genuine index signatures still contribute to the contextual type, including any keys. +declare const realIndex: RealIndex; +>realIndex : RealIndex + +declare const realMappedIndex: RealMappedIndex; +>realMappedIndex : RealMappedIndex + +declare const anyMappedIndex: AnyMappedIndex; +>anyMappedIndex : AnyMappedIndex + +realIndex.derive(() => ({ [key]: (el) => el })); +>realIndex.derive(() => ({ [key]: (el) => el })) : void +>realIndex.derive : (build: () => R) => void +>realIndex : RealIndex +>derive : (build: () => R) => void +>() => ({ [key]: (el) => el }) : () => { [key]: (el: any) => any; } +>({ [key]: (el) => el }) : { [key]: (el: any) => any; } +>{ [key]: (el) => el } : { [key]: (el: any) => any; } +>[key] : (el: any) => any +>key : string +>(el) => el : (el: any) => any +>el : any +>el : any + +realMappedIndex.derive(() => ({ [key]: (el) => el })); +>realMappedIndex.derive(() => ({ [key]: (el) => el })) : void +>realMappedIndex.derive : (build: () => R) => void +>realMappedIndex : RealMappedIndex +>derive : (build: () => R) => void +>() => ({ [key]: (el) => el }) : () => { [key]: (el: any) => any; } +>({ [key]: (el) => el }) : { [key]: (el: any) => any; } +>{ [key]: (el) => el } : { [key]: (el: any) => any; } +>[key] : (el: any) => any +>key : string +>(el) => el : (el: any) => any +>el : any +>el : any + +anyMappedIndex.derive(() => ({ [key]: (el) => el })); +>anyMappedIndex.derive(() => ({ [key]: (el) => el })) : void +>anyMappedIndex.derive : (build: () => R) => void +>anyMappedIndex : AnyMappedIndex +>derive : (build: () => R) => void +>() => ({ [key]: (el) => el }) : () => { [key]: (el: any) => any; } +>({ [key]: (el) => el }) : { [key]: (el: any) => any; } +>{ [key]: (el) => el } : { [key]: (el: any) => any; } +>[key] : (el: any) => any +>key : string +>(el) => el : (el: any) => any +>el : any +>el : any + +// An unresolved name is an error key type, but its constraint is not circular. +declare const unresolvedMappedIndex: UnresolvedMappedIndex; +>unresolvedMappedIndex : UnresolvedMappedIndex + +unresolvedMappedIndex.derive(() => ({ [key]: (el) => el })); +>unresolvedMappedIndex.derive(() => ({ [key]: (el) => el })) : void +>unresolvedMappedIndex.derive : (build: () => R) => void +>unresolvedMappedIndex : UnresolvedMappedIndex +>derive : (build: () => R) => void +>() => ({ [key]: (el) => el }) : () => { [key]: (el: any) => any; } +>({ [key]: (el) => el }) : { [key]: (el: any) => any; } +>{ [key]: (el) => el } : { [key]: (el: any) => any; } +>[key] : (el: any) => any +>key : string +>(el) => el : (el: any) => any +>el : any +>el : any + +const anyKeyedCallbacks: { [K in any]: (el: number) => unknown } = { +>anyKeyedCallbacks : { [x: string]: (el: number) => unknown; [x: number]: (el: number) => unknown; [x: symbol]: (el: number) => unknown; } +>el : number +>{ [key]: (el) => el.toFixed(),} : { [key]: (el: number) => string; } + + [key]: (el) => el.toFixed(), +>[key] : (el: number) => string +>key : string +>(el) => el.toFixed() : (el: number) => string +>el : number +>el.toFixed() : string +>el.toFixed : (fractionDigits?: number) => string +>el : number +>toFixed : (fractionDigits?: number) => string + +}; + diff --git a/tsc/testdata/tests/cases/compiler/contextualTypeWithCircularMappedKeyConstraint.ts b/tsc/testdata/tests/cases/compiler/contextualTypeWithCircularMappedKeyConstraint.ts new file mode 100644 index 0000000000000..4c115253de53b --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/contextualTypeWithCircularMappedKeyConstraint.ts @@ -0,0 +1,55 @@ +// @strict: true +// @skipLibCheck: true +// @noEmit: true + +// @filename: library.d.ts +type NoStrayRuleKeys = { [K in keyof R]: unknown }; +type StrictRules = ([keyof R] extends [never] ? Required : NoStrayRuleKeys) & { + [K in R extends unknown ? Exclude : never]?: never; +}; +export declare class Deriver { + derive>(build: () => R): void; + plain(build: () => R): void; +} + +export declare class RealIndex { + derive(build: () => R): void; +} +export declare class RealMappedIndex { + derive(build: () => R): void; +} +export declare class AnyMappedIndex { + derive(build: () => R): void; +} +export declare class UnresolvedMappedIndex { + derive(build: () => R): void; +} + +// @filename: main.ts +import type { Deriver, RealIndex, RealMappedIndex, AnyMappedIndex, UnresolvedMappedIndex } from "./library.js"; + +declare const key: string; +type Rule = { [x: string]: (el: number) => unknown }; +declare const indexed: Deriver; +declare const literal: Deriver<{ rows?: (el: number) => unknown }>; + +// Repro from #65530: a failed circular key constraint must not add an index signature. +indexed.derive(() => ({ [key]: (el) => el.toFixed() })); +literal.derive(() => ({ rows: (el) => el.toFixed() })); +indexed.plain(() => ({ [key]: (el) => el.toFixed() })); + +// Genuine index signatures still contribute to the contextual type, including any keys. +declare const realIndex: RealIndex; +declare const realMappedIndex: RealMappedIndex; +declare const anyMappedIndex: AnyMappedIndex; +realIndex.derive(() => ({ [key]: (el) => el })); +realMappedIndex.derive(() => ({ [key]: (el) => el })); +anyMappedIndex.derive(() => ({ [key]: (el) => el })); + +// An unresolved name is an error key type, but its constraint is not circular. +declare const unresolvedMappedIndex: UnresolvedMappedIndex; +unresolvedMappedIndex.derive(() => ({ [key]: (el) => el })); + +const anyKeyedCallbacks: { [K in any]: (el: number) => unknown } = { + [key]: (el) => el.toFixed(), +};