Skip to content

Fix #28798: Treat primitive key types as empty when relating to indexer from generic mapped type. #29699

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12785,9 +12785,10 @@ namespace ts {
(<IndexedAccessType>template).indexType === getTypeParameterFromMappedType(target)) {
return Ternary.True;
}

if (!isGenericMappedType(source)) {
const targetConstraint = getConstraintTypeFromMappedType(target);
const sourceKeys = getIndexType(source);
const sourceKeys = filterType(getIndexType(source), t => !(t.flags & (TypeFlags.String | TypeFlags.Number)));
const hasOptionalUnionKeys = modifiers & MappedTypeModifiers.IncludeOptional && targetConstraint.flags & TypeFlags.Union;
const filteredByApplicability = hasOptionalUnionKeys ? filterType(targetConstraint, t => !!isRelatedTo(t, sourceKeys)) : undefined;
// A source type T is related to a target type { [P in Q]: X } if Q is related to keyof T and T[Q] is related to X.
Expand Down
20 changes: 19 additions & 1 deletion tests/baselines/reference/indexSignatureAndMappedType.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ tests/cases/compiler/indexSignatureAndMappedType.ts(6,5): error TS2322: Type '{
tests/cases/compiler/indexSignatureAndMappedType.ts(15,5): error TS2322: Type 'Record<K, U>' is not assignable to type '{ [key: string]: T; }'.
Type 'U' is not assignable to type 'T'.
tests/cases/compiler/indexSignatureAndMappedType.ts(16,5): error TS2322: Type '{ [key: string]: T; }' is not assignable to type 'Record<K, U>'.
tests/cases/compiler/indexSignatureAndMappedType.ts(37,5): error TS2322: Type '{ [x: string]: number; }' is not assignable to type 'Record<K, number>'.
tests/cases/compiler/indexSignatureAndMappedType.ts(42,5): error TS2741: Property 'a' is missing in type '{ [x: string]: number; }' but required in type 'Record<"a", number>'.


==== tests/cases/compiler/indexSignatureAndMappedType.ts (3 errors) ====
==== tests/cases/compiler/indexSignatureAndMappedType.ts (5 errors) ====
// A mapped type { [P in K]: X }, where K is a generic type, is related to
// { [key: string]: Y } if X is related to Y.

Expand Down Expand Up @@ -44,4 +46,20 @@ tests/cases/compiler/indexSignatureAndMappedType.ts(16,5): error TS2322: Type '{
interface IEntity<T extends string> extends IBaseEntity {
properties: Record<T, string>;
}

// Repro from #28798

function constrainedRecord<K extends 'a'>(x: Record<K, number>, y: { [x: string]: number }) {
x = y; // error
~
!!! error TS2322: Type '{ [x: string]: number; }' is not assignable to type 'Record<K, number>'.
y = x;
}

function concreteRecord(x: Record<'a', number>, y: { [x: string]: number }) {
x = y; // error
~
!!! error TS2741: Property 'a' is missing in type '{ [x: string]: number; }' but required in type 'Record<"a", number>'.
y = x;
}

27 changes: 27 additions & 0 deletions tests/baselines/reference/indexSignatureAndMappedType.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ interface IBaseEntity {
interface IEntity<T extends string> extends IBaseEntity {
properties: Record<T, string>;
}

// Repro from #28798

function constrainedRecord<K extends 'a'>(x: Record<K, number>, y: { [x: string]: number }) {
x = y; // error
y = x;
}

function concreteRecord(x: Record<'a', number>, y: { [x: string]: number }) {
x = y; // error
y = x;
}


//// [indexSignatureAndMappedType.js]
Expand All @@ -49,6 +61,15 @@ function f3(x, y) {
x = y; // Error
y = x; // Error
}
// Repro from #28798
function constrainedRecord(x, y) {
x = y; // error
y = x;
}
function concreteRecord(x, y) {
x = y; // error
y = x;
}


//// [indexSignatureAndMappedType.d.ts]
Expand All @@ -71,3 +92,9 @@ interface IBaseEntity {
interface IEntity<T extends string> extends IBaseEntity {
properties: Record<T, string>;
}
declare function constrainedRecord<K extends 'a'>(x: Record<K, number>, y: {
[x: string]: number;
}): void;
declare function concreteRecord(x: Record<'a', number>, y: {
[x: string]: number;
}): void;
36 changes: 36 additions & 0 deletions tests/baselines/reference/indexSignatureAndMappedType.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,39 @@ interface IEntity<T extends string> extends IBaseEntity {
>T : Symbol(T, Decl(indexSignatureAndMappedType.ts, 29, 18))
}

// Repro from #28798

function constrainedRecord<K extends 'a'>(x: Record<K, number>, y: { [x: string]: number }) {
>constrainedRecord : Symbol(constrainedRecord, Decl(indexSignatureAndMappedType.ts, 31, 1))
>K : Symbol(K, Decl(indexSignatureAndMappedType.ts, 35, 27))
>x : Symbol(x, Decl(indexSignatureAndMappedType.ts, 35, 42))
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
>K : Symbol(K, Decl(indexSignatureAndMappedType.ts, 35, 27))
>y : Symbol(y, Decl(indexSignatureAndMappedType.ts, 35, 63))
>x : Symbol(x, Decl(indexSignatureAndMappedType.ts, 35, 70))

x = y; // error
>x : Symbol(x, Decl(indexSignatureAndMappedType.ts, 35, 42))
>y : Symbol(y, Decl(indexSignatureAndMappedType.ts, 35, 63))

y = x;
>y : Symbol(y, Decl(indexSignatureAndMappedType.ts, 35, 63))
>x : Symbol(x, Decl(indexSignatureAndMappedType.ts, 35, 42))
}

function concreteRecord(x: Record<'a', number>, y: { [x: string]: number }) {
>concreteRecord : Symbol(concreteRecord, Decl(indexSignatureAndMappedType.ts, 38, 1))
>x : Symbol(x, Decl(indexSignatureAndMappedType.ts, 40, 24))
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
>y : Symbol(y, Decl(indexSignatureAndMappedType.ts, 40, 47))
>x : Symbol(x, Decl(indexSignatureAndMappedType.ts, 40, 54))

x = y; // error
>x : Symbol(x, Decl(indexSignatureAndMappedType.ts, 40, 24))
>y : Symbol(y, Decl(indexSignatureAndMappedType.ts, 40, 47))

y = x;
>y : Symbol(y, Decl(indexSignatureAndMappedType.ts, 40, 47))
>x : Symbol(x, Decl(indexSignatureAndMappedType.ts, 40, 24))
}

36 changes: 36 additions & 0 deletions tests/baselines/reference/indexSignatureAndMappedType.types
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,39 @@ interface IEntity<T extends string> extends IBaseEntity {
>properties : Record<T, string>
}

// Repro from #28798

function constrainedRecord<K extends 'a'>(x: Record<K, number>, y: { [x: string]: number }) {
>constrainedRecord : <K extends "a">(x: Record<K, number>, y: { [x: string]: number; }) => void
>x : Record<K, number>
>y : { [x: string]: number; }
>x : string

x = y; // error
>x = y : { [x: string]: number; }
>x : Record<K, number>
>y : { [x: string]: number; }

y = x;
>y = x : Record<K, number>
>y : { [x: string]: number; }
>x : Record<K, number>
}

function concreteRecord(x: Record<'a', number>, y: { [x: string]: number }) {
>concreteRecord : (x: Record<"a", number>, y: { [x: string]: number; }) => void
>x : Record<"a", number>
>y : { [x: string]: number; }
>x : string

x = y; // error
>x = y : { [x: string]: number; }
>x : Record<"a", number>
>y : { [x: string]: number; }

y = x;
>y = x : Record<"a", number>
>y : { [x: string]: number; }
>x : Record<"a", number>
}

12 changes: 12 additions & 0 deletions tests/cases/compiler/indexSignatureAndMappedType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,15 @@ interface IBaseEntity {
interface IEntity<T extends string> extends IBaseEntity {
properties: Record<T, string>;
}

// Repro from #28798

function constrainedRecord<K extends 'a'>(x: Record<K, number>, y: { [x: string]: number }) {
x = y; // error
y = x;
}

function concreteRecord(x: Record<'a', number>, y: { [x: string]: number }) {
x = y; // error
y = x;
}