Skip to content

Fix index signature assignability from optional properties in --exactOptionalPropertyTypes mode #45185

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

Merged
merged 2 commits into from
Aug 6, 2021
Merged
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
4 changes: 2 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19436,8 +19436,8 @@ namespace ts {
continue;
}
if (isApplicableIndexType(getLiteralTypeFromProperty(prop, TypeFlags.StringOrNumberLiteralOrUnique), keyType)) {
const propType = getTypeOfSymbol(prop);
const type = propType.flags & TypeFlags.Undefined || keyType === numberType || !(prop.flags & SymbolFlags.Optional)
const propType = getNonMissingTypeOfSymbol(prop);
const type = exactOptionalPropertyTypes || propType.flags & TypeFlags.Undefined || keyType === numberType || !(prop.flags & SymbolFlags.Optional)
? propType
: getTypeWithFacts(propType, TypeFacts.NEUndefined);
const related = isRelatedTo(type, targetInfo.type, reportErrors);
Expand Down
31 changes: 30 additions & 1 deletion tests/baselines/reference/strictOptionalProperties1.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,16 @@ tests/cases/compiler/strictOptionalProperties1.ts(107,45): error TS2322: Type 'u
tests/cases/compiler/strictOptionalProperties1.ts(107,56): error TS2322: Type 'undefined' is not assignable to type 'boolean'.
tests/cases/compiler/strictOptionalProperties1.ts(111,31): error TS2322: Type 'undefined' is not assignable to type 'number'.
tests/cases/compiler/strictOptionalProperties1.ts(119,5): error TS2411: Property 'bar' of type 'string | undefined' is not assignable to 'string' index type 'string'.
tests/cases/compiler/strictOptionalProperties1.ts(192,1): error TS2322: Type '{ a: number; b: string | undefined; }' is not assignable to type '{ [x: string]: string | number; }'.
Property 'b' is incompatible with index signature.
Type 'string | undefined' is not assignable to type 'string | number'.
Type 'undefined' is not assignable to type 'string | number'.
tests/cases/compiler/strictOptionalProperties1.ts(193,1): error TS2322: Type '{ a: number; b?: string | undefined; }' is not assignable to type '{ [x: string]: string | number; }'.
Property 'b' is incompatible with index signature.
Type 'string | undefined' is not assignable to type 'string | number'.


==== tests/cases/compiler/strictOptionalProperties1.ts (17 errors) ====
==== tests/cases/compiler/strictOptionalProperties1.ts (19 errors) ====
function f1(obj: { a?: string, b?: string | undefined }) {
let a = obj.a; // string | undefined
let b = obj.b; // string | undefined
Expand Down Expand Up @@ -242,4 +249,26 @@ tests/cases/compiler/strictOptionalProperties1.ts(119,5): error TS2411: Property
}

declare function bb(input: number): void;

// Repro from #44437

declare var a: {[x: string]: number | string }
declare var b: {a: number, b: string}
declare var c: {a: number, b?: string}
declare var d: {a: number, b: string | undefined }
declare var e: {a: number, b?: string | undefined }

a = b;
a = c;
a = d; // Error
~
!!! error TS2322: Type '{ a: number; b: string | undefined; }' is not assignable to type '{ [x: string]: string | number; }'.
!!! error TS2322: Property 'b' is incompatible with index signature.
!!! error TS2322: Type 'string | undefined' is not assignable to type 'string | number'.
!!! error TS2322: Type 'undefined' is not assignable to type 'string | number'.
a = e; // Error
~
!!! error TS2322: Type '{ a: number; b?: string | undefined; }' is not assignable to type '{ [x: string]: string | number; }'.
!!! error TS2322: Property 'b' is incompatible with index signature.
!!! error TS2322: Type 'string | undefined' is not assignable to type 'string | number'.

36 changes: 36 additions & 0 deletions tests/baselines/reference/strictOptionalProperties1.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,19 @@ function aa(input: Bar): void {
}

declare function bb(input: number): void;

// Repro from #44437

declare var a: {[x: string]: number | string }
declare var b: {a: number, b: string}
declare var c: {a: number, b?: string}
declare var d: {a: number, b: string | undefined }
declare var e: {a: number, b?: string | undefined }

a = b;
a = c;
a = d; // Error
a = e; // Error


//// [strictOptionalProperties1.js]
Expand Down Expand Up @@ -309,6 +322,10 @@ function aa(input) {
var notUndefinedVal = expectNotUndefined(input.bar);
bb(notUndefinedVal);
}
a = b;
a = c;
a = d; // Error
a = e; // Error


//// [strictOptionalProperties1.d.ts]
Expand Down Expand Up @@ -382,3 +399,22 @@ interface Bar {
}
declare function aa(input: Bar): void;
declare function bb(input: number): void;
declare var a: {
[x: string]: number | string;
};
declare var b: {
a: number;
b: string;
};
declare var c: {
a: number;
b?: string;
};
declare var d: {
a: number;
b: string | undefined;
};
declare var e: {
a: number;
b?: string | undefined;
};
42 changes: 42 additions & 0 deletions tests/baselines/reference/strictOptionalProperties1.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -571,3 +571,45 @@ declare function bb(input: number): void;
>bb : Symbol(bb, Decl(strictOptionalProperties1.ts, 177, 1))
>input : Symbol(input, Decl(strictOptionalProperties1.ts, 179, 20))

// Repro from #44437

declare var a: {[x: string]: number | string }
>a : Symbol(a, Decl(strictOptionalProperties1.ts, 183, 11))
>x : Symbol(x, Decl(strictOptionalProperties1.ts, 183, 17))

declare var b: {a: number, b: string}
>b : Symbol(b, Decl(strictOptionalProperties1.ts, 184, 11))
>a : Symbol(a, Decl(strictOptionalProperties1.ts, 184, 16))
>b : Symbol(b, Decl(strictOptionalProperties1.ts, 184, 26))

declare var c: {a: number, b?: string}
>c : Symbol(c, Decl(strictOptionalProperties1.ts, 185, 11))
>a : Symbol(a, Decl(strictOptionalProperties1.ts, 185, 16))
>b : Symbol(b, Decl(strictOptionalProperties1.ts, 185, 26))

declare var d: {a: number, b: string | undefined }
>d : Symbol(d, Decl(strictOptionalProperties1.ts, 186, 11))
>a : Symbol(a, Decl(strictOptionalProperties1.ts, 186, 16))
>b : Symbol(b, Decl(strictOptionalProperties1.ts, 186, 26))

declare var e: {a: number, b?: string | undefined }
>e : Symbol(e, Decl(strictOptionalProperties1.ts, 187, 11))
>a : Symbol(a, Decl(strictOptionalProperties1.ts, 187, 16))
>b : Symbol(b, Decl(strictOptionalProperties1.ts, 187, 26))

a = b;
>a : Symbol(a, Decl(strictOptionalProperties1.ts, 183, 11))
>b : Symbol(b, Decl(strictOptionalProperties1.ts, 184, 11))

a = c;
>a : Symbol(a, Decl(strictOptionalProperties1.ts, 183, 11))
>c : Symbol(c, Decl(strictOptionalProperties1.ts, 185, 11))

a = d; // Error
>a : Symbol(a, Decl(strictOptionalProperties1.ts, 183, 11))
>d : Symbol(d, Decl(strictOptionalProperties1.ts, 186, 11))

a = e; // Error
>a : Symbol(a, Decl(strictOptionalProperties1.ts, 183, 11))
>e : Symbol(e, Decl(strictOptionalProperties1.ts, 187, 11))

46 changes: 46 additions & 0 deletions tests/baselines/reference/strictOptionalProperties1.types
Original file line number Diff line number Diff line change
Expand Up @@ -683,3 +683,49 @@ declare function bb(input: number): void;
>bb : (input: number) => void
>input : number

// Repro from #44437

declare var a: {[x: string]: number | string }
>a : { [x: string]: string | number; }
>x : string

declare var b: {a: number, b: string}
>b : { a: number; b: string; }
>a : number
>b : string

declare var c: {a: number, b?: string}
>c : { a: number; b?: string; }
>a : number
>b : string | undefined

declare var d: {a: number, b: string | undefined }
>d : { a: number; b: string | undefined; }
>a : number
>b : string | undefined

declare var e: {a: number, b?: string | undefined }
>e : { a: number; b?: string | undefined; }
>a : number
>b : string | undefined

a = b;
>a = b : { a: number; b: string; }
>a : { [x: string]: string | number; }
>b : { a: number; b: string; }

a = c;
>a = c : { a: number; b?: string; }
>a : { [x: string]: string | number; }
>c : { a: number; b?: string; }

a = d; // Error
>a = d : { a: number; b: string | undefined; }
>a : { [x: string]: string | number; }
>d : { a: number; b: string | undefined; }

a = e; // Error
>a = e : { a: number; b?: string | undefined; }
>a : { [x: string]: string | number; }
>e : { a: number; b?: string | undefined; }

13 changes: 13 additions & 0 deletions tests/cases/compiler/strictOptionalProperties1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,16 @@ function aa(input: Bar): void {
}

declare function bb(input: number): void;

// Repro from #44437

declare var a: {[x: string]: number | string }
declare var b: {a: number, b: string}
declare var c: {a: number, b?: string}
declare var d: {a: number, b: string | undefined }
declare var e: {a: number, b?: string | undefined }

a = b;
a = c;
a = d; // Error
a = e; // Error