Skip to content

Check combined intersection properties against target index signatures #35143

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 8 commits into from
Jan 10, 2020
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
156 changes: 78 additions & 78 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
tests/cases/conformance/types/intersection/intersectionWithIndexSignatures.ts(17,1): error TS2322: Type '{ x: A; } & { y: B; }' is not assignable to type '{ [key: string]: A; }'.
Property 'y' is incompatible with index signature.
Property 'a' is missing in type 'B' but required in type 'A'.
tests/cases/conformance/types/intersection/intersectionWithIndexSignatures.ts(27,10): error TS2339: Property 'b' does not exist on type '{ a: string; }'.
tests/cases/conformance/types/intersection/intersectionWithIndexSignatures.ts(29,7): error TS2322: Type 'constr<{}, { [key: string]: { a: string; }; }>' is not assignable to type '{ [key: string]: { a: string; b: string; }; }'.
Index signatures are incompatible.
Property 'b' is missing in type '{ a: string; }' but required in type '{ a: string; b: string; }'.
tests/cases/conformance/types/intersection/intersectionWithIndexSignatures.ts(35,1): error TS2322: Type '{ a: string; } & { b: number; }' is not assignable to type '{ [key: string]: string; }'.
Property 'b' is incompatible with index signature.
Type 'number' is not assignable to type 'string'.


==== tests/cases/conformance/types/intersection/intersectionWithIndexSignatures.ts (4 errors) ====
type A = { a: string };
type B = { b: string };

declare let sa1: { x: A & B };
declare let sa2: { x: A } & { x: B };
declare let ta1: { [key: string]: A & B };
declare let ta2: { [key: string]: A } & { [key: string]: B };

ta1 = sa1;
ta1 = sa2;
ta2 = sa1;
ta2 = sa2;

declare let sb1: { x: A } & { y: B };
declare let tb1: { [key: string]: A };

tb1 = sb1; // Error
~~~
!!! error TS2322: Type '{ x: A; } & { y: B; }' is not assignable to type '{ [key: string]: A; }'.
!!! error TS2322: Property 'y' is incompatible with index signature.
!!! error TS2322: Property 'a' is missing in type 'B' but required in type 'A'.
!!! related TS2728 tests/cases/conformance/types/intersection/intersectionWithIndexSignatures.ts:1:12: 'a' is declared here.

// Repro from #32484

type constr<Source, Tgt> = { [K in keyof Source]: string } & Pick<Tgt, Exclude<keyof Tgt, keyof Source>>;

type s = constr<{}, { [key: string]: { a: string } }>;

declare const q: s;
q["asd"].a.substr(1);
q["asd"].b; // Error
~
!!! error TS2339: Property 'b' does not exist on type '{ a: string; }'.

const d: { [key: string]: {a: string, b: string} } = q; // Error
~
!!! error TS2322: Type 'constr<{}, { [key: string]: { a: string; }; }>' is not assignable to type '{ [key: string]: { a: string; b: string; }; }'.
!!! error TS2322: Index signatures are incompatible.
!!! error TS2322: Property 'b' is missing in type '{ a: string; }' but required in type '{ a: string; b: string; }'.
!!! related TS2728 tests/cases/conformance/types/intersection/intersectionWithIndexSignatures.ts:29:39: 'b' is declared here.

// Repro from #32484

declare let ss: { a: string } & { b: number };
declare let tt: { [key: string]: string };
tt = ss; // Error
~~
!!! error TS2322: Type '{ a: string; } & { b: number; }' is not assignable to type '{ [key: string]: string; }'.
!!! error TS2322: Property 'b' is incompatible with index signature.
!!! error TS2322: Type 'number' is not assignable to type 'string'.

49 changes: 49 additions & 0 deletions tests/baselines/reference/intersectionWithIndexSignatures.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//// [intersectionWithIndexSignatures.ts]
type A = { a: string };
type B = { b: string };

declare let sa1: { x: A & B };
declare let sa2: { x: A } & { x: B };
declare let ta1: { [key: string]: A & B };
declare let ta2: { [key: string]: A } & { [key: string]: B };

ta1 = sa1;
ta1 = sa2;
ta2 = sa1;
ta2 = sa2;

declare let sb1: { x: A } & { y: B };
declare let tb1: { [key: string]: A };

tb1 = sb1; // Error

// Repro from #32484

type constr<Source, Tgt> = { [K in keyof Source]: string } & Pick<Tgt, Exclude<keyof Tgt, keyof Source>>;

type s = constr<{}, { [key: string]: { a: string } }>;

declare const q: s;
q["asd"].a.substr(1);
q["asd"].b; // Error

const d: { [key: string]: {a: string, b: string} } = q; // Error

// Repro from #32484

declare let ss: { a: string } & { b: number };
declare let tt: { [key: string]: string };
tt = ss; // Error


//// [intersectionWithIndexSignatures.js]
"use strict";
ta1 = sa1;
ta1 = sa2;
ta2 = sa1;
ta2 = sa2;
tb1 = sb1; // Error
q["asd"].a.substr(1);
q["asd"].b; // Error
var d = q; // Error
tt = ss; // Error
123 changes: 123 additions & 0 deletions tests/baselines/reference/intersectionWithIndexSignatures.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
=== tests/cases/conformance/types/intersection/intersectionWithIndexSignatures.ts ===
type A = { a: string };
>A : Symbol(A, Decl(intersectionWithIndexSignatures.ts, 0, 0))
>a : Symbol(a, Decl(intersectionWithIndexSignatures.ts, 0, 10))

type B = { b: string };
>B : Symbol(B, Decl(intersectionWithIndexSignatures.ts, 0, 23))
>b : Symbol(b, Decl(intersectionWithIndexSignatures.ts, 1, 10))

declare let sa1: { x: A & B };
>sa1 : Symbol(sa1, Decl(intersectionWithIndexSignatures.ts, 3, 11))
>x : Symbol(x, Decl(intersectionWithIndexSignatures.ts, 3, 18))
>A : Symbol(A, Decl(intersectionWithIndexSignatures.ts, 0, 0))
>B : Symbol(B, Decl(intersectionWithIndexSignatures.ts, 0, 23))

declare let sa2: { x: A } & { x: B };
>sa2 : Symbol(sa2, Decl(intersectionWithIndexSignatures.ts, 4, 11))
>x : Symbol(x, Decl(intersectionWithIndexSignatures.ts, 4, 18))
>A : Symbol(A, Decl(intersectionWithIndexSignatures.ts, 0, 0))
>x : Symbol(x, Decl(intersectionWithIndexSignatures.ts, 4, 29))
>B : Symbol(B, Decl(intersectionWithIndexSignatures.ts, 0, 23))

declare let ta1: { [key: string]: A & B };
>ta1 : Symbol(ta1, Decl(intersectionWithIndexSignatures.ts, 5, 11))
>key : Symbol(key, Decl(intersectionWithIndexSignatures.ts, 5, 20))
>A : Symbol(A, Decl(intersectionWithIndexSignatures.ts, 0, 0))
>B : Symbol(B, Decl(intersectionWithIndexSignatures.ts, 0, 23))

declare let ta2: { [key: string]: A } & { [key: string]: B };
>ta2 : Symbol(ta2, Decl(intersectionWithIndexSignatures.ts, 6, 11))
>key : Symbol(key, Decl(intersectionWithIndexSignatures.ts, 6, 20))
>A : Symbol(A, Decl(intersectionWithIndexSignatures.ts, 0, 0))
>key : Symbol(key, Decl(intersectionWithIndexSignatures.ts, 6, 43))
>B : Symbol(B, Decl(intersectionWithIndexSignatures.ts, 0, 23))

ta1 = sa1;
>ta1 : Symbol(ta1, Decl(intersectionWithIndexSignatures.ts, 5, 11))
>sa1 : Symbol(sa1, Decl(intersectionWithIndexSignatures.ts, 3, 11))

ta1 = sa2;
>ta1 : Symbol(ta1, Decl(intersectionWithIndexSignatures.ts, 5, 11))
>sa2 : Symbol(sa2, Decl(intersectionWithIndexSignatures.ts, 4, 11))

ta2 = sa1;
>ta2 : Symbol(ta2, Decl(intersectionWithIndexSignatures.ts, 6, 11))
>sa1 : Symbol(sa1, Decl(intersectionWithIndexSignatures.ts, 3, 11))

ta2 = sa2;
>ta2 : Symbol(ta2, Decl(intersectionWithIndexSignatures.ts, 6, 11))
>sa2 : Symbol(sa2, Decl(intersectionWithIndexSignatures.ts, 4, 11))

declare let sb1: { x: A } & { y: B };
>sb1 : Symbol(sb1, Decl(intersectionWithIndexSignatures.ts, 13, 11))
>x : Symbol(x, Decl(intersectionWithIndexSignatures.ts, 13, 18))
>A : Symbol(A, Decl(intersectionWithIndexSignatures.ts, 0, 0))
>y : Symbol(y, Decl(intersectionWithIndexSignatures.ts, 13, 29))
>B : Symbol(B, Decl(intersectionWithIndexSignatures.ts, 0, 23))

declare let tb1: { [key: string]: A };
>tb1 : Symbol(tb1, Decl(intersectionWithIndexSignatures.ts, 14, 11))
>key : Symbol(key, Decl(intersectionWithIndexSignatures.ts, 14, 20))
>A : Symbol(A, Decl(intersectionWithIndexSignatures.ts, 0, 0))

tb1 = sb1; // Error
>tb1 : Symbol(tb1, Decl(intersectionWithIndexSignatures.ts, 14, 11))
>sb1 : Symbol(sb1, Decl(intersectionWithIndexSignatures.ts, 13, 11))

// Repro from #32484

type constr<Source, Tgt> = { [K in keyof Source]: string } & Pick<Tgt, Exclude<keyof Tgt, keyof Source>>;
>constr : Symbol(constr, Decl(intersectionWithIndexSignatures.ts, 16, 10))
>Source : Symbol(Source, Decl(intersectionWithIndexSignatures.ts, 20, 12))
>Tgt : Symbol(Tgt, Decl(intersectionWithIndexSignatures.ts, 20, 19))
>K : Symbol(K, Decl(intersectionWithIndexSignatures.ts, 20, 30))
>Source : Symbol(Source, Decl(intersectionWithIndexSignatures.ts, 20, 12))
>Pick : Symbol(Pick, Decl(lib.es5.d.ts, --, --))
>Tgt : Symbol(Tgt, Decl(intersectionWithIndexSignatures.ts, 20, 19))
>Exclude : Symbol(Exclude, Decl(lib.es5.d.ts, --, --))
>Tgt : Symbol(Tgt, Decl(intersectionWithIndexSignatures.ts, 20, 19))
>Source : Symbol(Source, Decl(intersectionWithIndexSignatures.ts, 20, 12))

type s = constr<{}, { [key: string]: { a: string } }>;
>s : Symbol(s, Decl(intersectionWithIndexSignatures.ts, 20, 105))
>constr : Symbol(constr, Decl(intersectionWithIndexSignatures.ts, 16, 10))
>key : Symbol(key, Decl(intersectionWithIndexSignatures.ts, 22, 23))
>a : Symbol(a, Decl(intersectionWithIndexSignatures.ts, 22, 38))

declare const q: s;
>q : Symbol(q, Decl(intersectionWithIndexSignatures.ts, 24, 13))
>s : Symbol(s, Decl(intersectionWithIndexSignatures.ts, 20, 105))

q["asd"].a.substr(1);
>q["asd"].a.substr : Symbol(String.substr, Decl(lib.es5.d.ts, --, --))
>q["asd"].a : Symbol(a, Decl(intersectionWithIndexSignatures.ts, 22, 38))
>q : Symbol(q, Decl(intersectionWithIndexSignatures.ts, 24, 13))
>a : Symbol(a, Decl(intersectionWithIndexSignatures.ts, 22, 38))
>substr : Symbol(String.substr, Decl(lib.es5.d.ts, --, --))

q["asd"].b; // Error
>q : Symbol(q, Decl(intersectionWithIndexSignatures.ts, 24, 13))

const d: { [key: string]: {a: string, b: string} } = q; // Error
>d : Symbol(d, Decl(intersectionWithIndexSignatures.ts, 28, 5))
>key : Symbol(key, Decl(intersectionWithIndexSignatures.ts, 28, 12))
>a : Symbol(a, Decl(intersectionWithIndexSignatures.ts, 28, 27))
>b : Symbol(b, Decl(intersectionWithIndexSignatures.ts, 28, 37))
>q : Symbol(q, Decl(intersectionWithIndexSignatures.ts, 24, 13))

// Repro from #32484

declare let ss: { a: string } & { b: number };
>ss : Symbol(ss, Decl(intersectionWithIndexSignatures.ts, 32, 11))
>a : Symbol(a, Decl(intersectionWithIndexSignatures.ts, 32, 17))
>b : Symbol(b, Decl(intersectionWithIndexSignatures.ts, 32, 33))

declare let tt: { [key: string]: string };
>tt : Symbol(tt, Decl(intersectionWithIndexSignatures.ts, 33, 11))
>key : Symbol(key, Decl(intersectionWithIndexSignatures.ts, 33, 19))

tt = ss; // Error
>tt : Symbol(tt, Decl(intersectionWithIndexSignatures.ts, 33, 11))
>ss : Symbol(ss, Decl(intersectionWithIndexSignatures.ts, 32, 11))

115 changes: 115 additions & 0 deletions tests/baselines/reference/intersectionWithIndexSignatures.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
=== tests/cases/conformance/types/intersection/intersectionWithIndexSignatures.ts ===
type A = { a: string };
>A : A
>a : string

type B = { b: string };
>B : B
>b : string

declare let sa1: { x: A & B };
>sa1 : { x: A & B; }
>x : A & B

declare let sa2: { x: A } & { x: B };
>sa2 : { x: A; } & { x: B; }
>x : A
>x : B

declare let ta1: { [key: string]: A & B };
>ta1 : { [key: string]: A & B; }
>key : string

declare let ta2: { [key: string]: A } & { [key: string]: B };
>ta2 : { [key: string]: A; } & { [key: string]: B; }
>key : string
>key : string

ta1 = sa1;
>ta1 = sa1 : { x: A & B; }
>ta1 : { [key: string]: A & B; }
>sa1 : { x: A & B; }

ta1 = sa2;
>ta1 = sa2 : { x: A; } & { x: B; }
>ta1 : { [key: string]: A & B; }
>sa2 : { x: A; } & { x: B; }

ta2 = sa1;
>ta2 = sa1 : { x: A & B; }
>ta2 : { [key: string]: A; } & { [key: string]: B; }
>sa1 : { x: A & B; }

ta2 = sa2;
>ta2 = sa2 : { x: A; } & { x: B; }
>ta2 : { [key: string]: A; } & { [key: string]: B; }
>sa2 : { x: A; } & { x: B; }

declare let sb1: { x: A } & { y: B };
>sb1 : { x: A; } & { y: B; }
>x : A
>y : B

declare let tb1: { [key: string]: A };
>tb1 : { [key: string]: A; }
>key : string

tb1 = sb1; // Error
>tb1 = sb1 : { x: A; } & { y: B; }
>tb1 : { [key: string]: A; }
>sb1 : { x: A; } & { y: B; }

// Repro from #32484

type constr<Source, Tgt> = { [K in keyof Source]: string } & Pick<Tgt, Exclude<keyof Tgt, keyof Source>>;
>constr : constr<Source, Tgt>

type s = constr<{}, { [key: string]: { a: string } }>;
>s : constr<{}, { [key: string]: { a: string; }; }>
>key : string
>a : string

declare const q: s;
>q : constr<{}, { [key: string]: { a: string; }; }>

q["asd"].a.substr(1);
>q["asd"].a.substr(1) : string
>q["asd"].a.substr : (from: number, length?: number | undefined) => string
>q["asd"].a : string
>q["asd"] : { a: string; }
>q : constr<{}, { [key: string]: { a: string; }; }>
>"asd" : "asd"
>a : string
>substr : (from: number, length?: number | undefined) => string
>1 : 1

q["asd"].b; // Error
>q["asd"].b : any
>q["asd"] : { a: string; }
>q : constr<{}, { [key: string]: { a: string; }; }>
>"asd" : "asd"
>b : any

const d: { [key: string]: {a: string, b: string} } = q; // Error
>d : { [key: string]: { a: string; b: string; }; }
>key : string
>a : string
>b : string
>q : constr<{}, { [key: string]: { a: string; }; }>

// Repro from #32484

declare let ss: { a: string } & { b: number };
>ss : { a: string; } & { b: number; }
>a : string
>b : number

declare let tt: { [key: string]: string };
>tt : { [key: string]: string; }
>key : string

tt = ss; // Error
>tt = ss : { a: string; } & { b: number; }
>tt : { [key: string]: string; }
>ss : { a: string; } & { b: number; }

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// @strict: true

type A = { a: string };
type B = { b: string };

declare let sa1: { x: A & B };
declare let sa2: { x: A } & { x: B };
declare let ta1: { [key: string]: A & B };
declare let ta2: { [key: string]: A } & { [key: string]: B };

ta1 = sa1;
ta1 = sa2;
ta2 = sa1;
ta2 = sa2;

declare let sb1: { x: A } & { y: B };
declare let tb1: { [key: string]: A };

tb1 = sb1; // Error

// Repro from #32484

type constr<Source, Tgt> = { [K in keyof Source]: string } & Pick<Tgt, Exclude<keyof Tgt, keyof Source>>;

type s = constr<{}, { [key: string]: { a: string } }>;

declare const q: s;
q["asd"].a.substr(1);
q["asd"].b; // Error

const d: { [key: string]: {a: string, b: string} } = q; // Error

// Repro from #32484

declare let ss: { a: string } & { b: number };
declare let tt: { [key: string]: string };
tt = ss; // Error