diff --git a/tests/baselines/reference/mappedTypeRelationships.errors.txt b/tests/baselines/reference/mappedTypeRelationships.errors.txt new file mode 100644 index 00000000000..adafbc5f3a7 --- /dev/null +++ b/tests/baselines/reference/mappedTypeRelationships.errors.txt @@ -0,0 +1,165 @@ +tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(12,5): error TS2322: Type 'T[keyof T]' is not assignable to type 'U[keyof T]'. + Type 'T' is not assignable to type 'U'. +tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(17,5): error TS2322: Type 'T[K]' is not assignable to type 'U[K]'. + Type 'T' is not assignable to type 'U'. +tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(21,5): error TS2536: Type 'keyof U' cannot be used to index type 'T'. +tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(22,12): error TS2536: Type 'keyof U' cannot be used to index type 'T'. +tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(26,5): error TS2536: Type 'K' cannot be used to index type 'T'. +tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(27,12): error TS2536: Type 'K' cannot be used to index type 'T'. +tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(31,5): error TS2322: Type 'T[keyof T] | undefined' is not assignable to type 'T[keyof T]'. + Type 'undefined' is not assignable to type 'T[keyof T]'. +tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(36,5): error TS2322: Type 'T[K] | undefined' is not assignable to type 'T[K]'. + Type 'undefined' is not assignable to type 'T[K]'. +tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(41,5): error TS2322: Type 'U[keyof T] | undefined' is not assignable to type 'T[keyof T]'. + Type 'undefined' is not assignable to type 'T[keyof T]'. +tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(42,5): error TS2322: Type 'T[keyof T]' is not assignable to type 'U[keyof T] | undefined'. + Type 'T[keyof T]' is not assignable to type 'U[keyof T]'. + Type 'T' is not assignable to type 'U'. +tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(46,5): error TS2322: Type 'U[K] | undefined' is not assignable to type 'T[K]'. + Type 'undefined' is not assignable to type 'T[K]'. +tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(47,5): error TS2322: Type 'T[K]' is not assignable to type 'U[K] | undefined'. + Type 'T[K]' is not assignable to type 'U[K]'. + Type 'T' is not assignable to type 'U'. +tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(52,5): error TS2542: Index signature in type 'Readonly' only permits reading. +tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(57,5): error TS2542: Index signature in type 'Readonly' only permits reading. +tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(62,5): error TS2542: Index signature in type 'Readonly' only permits reading. +tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(67,5): error TS2542: Index signature in type 'Readonly' only permits reading. +tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(71,5): error TS2322: Type 'Partial' is not assignable to type 'T'. +tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(76,5): error TS2322: Type 'Partial' is not assignable to type 'T'. + + +==== tests/cases/conformance/types/mapped/mappedTypeRelationships.ts (18 errors) ==== + + function f1(x: T, k: keyof T) { + return x[k]; + } + + function f2(x: T, k: K) { + return x[k]; + } + + function f3(x: T, y: U, k: keyof T) { + x[k] = y[k]; + y[k] = x[k]; // Error + ~~~~ +!!! error TS2322: Type 'T[keyof T]' is not assignable to type 'U[keyof T]'. +!!! error TS2322: Type 'T' is not assignable to type 'U'. + } + + function f4(x: T, y: U, k: K) { + x[k] = y[k]; + y[k] = x[k]; // Error + ~~~~ +!!! error TS2322: Type 'T[K]' is not assignable to type 'U[K]'. +!!! error TS2322: Type 'T' is not assignable to type 'U'. + } + + function f5(x: T, y: U, k: keyof U) { + x[k] = y[k]; // Error + ~~~~ +!!! error TS2536: Type 'keyof U' cannot be used to index type 'T'. + y[k] = x[k]; // Error + ~~~~ +!!! error TS2536: Type 'keyof U' cannot be used to index type 'T'. + } + + function f6(x: T, y: U, k: K) { + x[k] = y[k]; // Error + ~~~~ +!!! error TS2536: Type 'K' cannot be used to index type 'T'. + y[k] = x[k]; // Error + ~~~~ +!!! error TS2536: Type 'K' cannot be used to index type 'T'. + } + + function f10(x: T, y: Partial, k: keyof T) { + x[k] = y[k]; // Error + ~~~~ +!!! error TS2322: Type 'T[keyof T] | undefined' is not assignable to type 'T[keyof T]'. +!!! error TS2322: Type 'undefined' is not assignable to type 'T[keyof T]'. + y[k] = x[k]; + } + + function f11(x: T, y: Partial, k: K) { + x[k] = y[k]; // Error + ~~~~ +!!! error TS2322: Type 'T[K] | undefined' is not assignable to type 'T[K]'. +!!! error TS2322: Type 'undefined' is not assignable to type 'T[K]'. + y[k] = x[k]; + } + + function f12(x: T, y: Partial, k: keyof T) { + x[k] = y[k]; // Error + ~~~~ +!!! error TS2322: Type 'U[keyof T] | undefined' is not assignable to type 'T[keyof T]'. +!!! error TS2322: Type 'undefined' is not assignable to type 'T[keyof T]'. + y[k] = x[k]; // Error + ~~~~ +!!! error TS2322: Type 'T[keyof T]' is not assignable to type 'U[keyof T] | undefined'. +!!! error TS2322: Type 'T[keyof T]' is not assignable to type 'U[keyof T]'. +!!! error TS2322: Type 'T' is not assignable to type 'U'. + } + + function f13(x: T, y: Partial, k: K) { + x[k] = y[k]; // Error + ~~~~ +!!! error TS2322: Type 'U[K] | undefined' is not assignable to type 'T[K]'. +!!! error TS2322: Type 'undefined' is not assignable to type 'T[K]'. + y[k] = x[k]; // Error + ~~~~ +!!! error TS2322: Type 'T[K]' is not assignable to type 'U[K] | undefined'. +!!! error TS2322: Type 'T[K]' is not assignable to type 'U[K]'. +!!! error TS2322: Type 'T' is not assignable to type 'U'. + } + + function f20(x: T, y: Readonly, k: keyof T) { + x[k] = y[k]; + y[k] = x[k]; // Error + ~~~~ +!!! error TS2542: Index signature in type 'Readonly' only permits reading. + } + + function f21(x: T, y: Readonly, k: K) { + x[k] = y[k]; + y[k] = x[k]; // Error + ~~~~ +!!! error TS2542: Index signature in type 'Readonly' only permits reading. + } + + function f22(x: T, y: Readonly, k: keyof T) { + x[k] = y[k]; + y[k] = x[k]; // Error + ~~~~ +!!! error TS2542: Index signature in type 'Readonly' only permits reading. + } + + function f23(x: T, y: Readonly, k: K) { + x[k] = y[k]; + y[k] = x[k]; // Error + ~~~~ +!!! error TS2542: Index signature in type 'Readonly' only permits reading. + } + + function f30(x: T, y: Partial) { + x = y; // Error + ~ +!!! error TS2322: Type 'Partial' is not assignable to type 'T'. + y = x; + } + + function f31(x: T, y: Partial) { + x = y; // Error + ~ +!!! error TS2322: Type 'Partial' is not assignable to type 'T'. + y = x; + } + + function f40(x: T, y: Readonly) { + x = y; + y = x; + } + + function f41(x: T, y: Readonly) { + x = y; + y = x; + } \ No newline at end of file diff --git a/tests/baselines/reference/mappedTypeRelationships.js b/tests/baselines/reference/mappedTypeRelationships.js new file mode 100644 index 00000000000..fcad6973ab7 --- /dev/null +++ b/tests/baselines/reference/mappedTypeRelationships.js @@ -0,0 +1,182 @@ +//// [mappedTypeRelationships.ts] + +function f1(x: T, k: keyof T) { + return x[k]; +} + +function f2(x: T, k: K) { + return x[k]; +} + +function f3(x: T, y: U, k: keyof T) { + x[k] = y[k]; + y[k] = x[k]; // Error +} + +function f4(x: T, y: U, k: K) { + x[k] = y[k]; + y[k] = x[k]; // Error +} + +function f5(x: T, y: U, k: keyof U) { + x[k] = y[k]; // Error + y[k] = x[k]; // Error +} + +function f6(x: T, y: U, k: K) { + x[k] = y[k]; // Error + y[k] = x[k]; // Error +} + +function f10(x: T, y: Partial, k: keyof T) { + x[k] = y[k]; // Error + y[k] = x[k]; +} + +function f11(x: T, y: Partial, k: K) { + x[k] = y[k]; // Error + y[k] = x[k]; +} + +function f12(x: T, y: Partial, k: keyof T) { + x[k] = y[k]; // Error + y[k] = x[k]; // Error +} + +function f13(x: T, y: Partial, k: K) { + x[k] = y[k]; // Error + y[k] = x[k]; // Error +} + +function f20(x: T, y: Readonly, k: keyof T) { + x[k] = y[k]; + y[k] = x[k]; // Error +} + +function f21(x: T, y: Readonly, k: K) { + x[k] = y[k]; + y[k] = x[k]; // Error +} + +function f22(x: T, y: Readonly, k: keyof T) { + x[k] = y[k]; + y[k] = x[k]; // Error +} + +function f23(x: T, y: Readonly, k: K) { + x[k] = y[k]; + y[k] = x[k]; // Error +} + +function f30(x: T, y: Partial) { + x = y; // Error + y = x; +} + +function f31(x: T, y: Partial) { + x = y; // Error + y = x; +} + +function f40(x: T, y: Readonly) { + x = y; + y = x; +} + +function f41(x: T, y: Readonly) { + x = y; + y = x; +} + +//// [mappedTypeRelationships.js] +function f1(x, k) { + return x[k]; +} +function f2(x, k) { + return x[k]; +} +function f3(x, y, k) { + x[k] = y[k]; + y[k] = x[k]; // Error +} +function f4(x, y, k) { + x[k] = y[k]; + y[k] = x[k]; // Error +} +function f5(x, y, k) { + x[k] = y[k]; // Error + y[k] = x[k]; // Error +} +function f6(x, y, k) { + x[k] = y[k]; // Error + y[k] = x[k]; // Error +} +function f10(x, y, k) { + x[k] = y[k]; // Error + y[k] = x[k]; +} +function f11(x, y, k) { + x[k] = y[k]; // Error + y[k] = x[k]; +} +function f12(x, y, k) { + x[k] = y[k]; // Error + y[k] = x[k]; // Error +} +function f13(x, y, k) { + x[k] = y[k]; // Error + y[k] = x[k]; // Error +} +function f20(x, y, k) { + x[k] = y[k]; + y[k] = x[k]; // Error +} +function f21(x, y, k) { + x[k] = y[k]; + y[k] = x[k]; // Error +} +function f22(x, y, k) { + x[k] = y[k]; + y[k] = x[k]; // Error +} +function f23(x, y, k) { + x[k] = y[k]; + y[k] = x[k]; // Error +} +function f30(x, y) { + x = y; // Error + y = x; +} +function f31(x, y) { + x = y; // Error + y = x; +} +function f40(x, y) { + x = y; + y = x; +} +function f41(x, y) { + x = y; + y = x; +} + + +//// [mappedTypeRelationships.d.ts] +declare function f1(x: T, k: keyof T): T[keyof T]; +declare function f2(x: T, k: K): T[K]; +declare function f3(x: T, y: U, k: keyof T): void; +declare function f4(x: T, y: U, k: K): void; +declare function f5(x: T, y: U, k: keyof U): void; +declare function f6(x: T, y: U, k: K): void; +declare function f10(x: T, y: Partial, k: keyof T): void; +declare function f11(x: T, y: Partial, k: K): void; +declare function f12(x: T, y: Partial, k: keyof T): void; +declare function f13(x: T, y: Partial, k: K): void; +declare function f20(x: T, y: Readonly, k: keyof T): void; +declare function f21(x: T, y: Readonly, k: K): void; +declare function f22(x: T, y: Readonly, k: keyof T): void; +declare function f23(x: T, y: Readonly, k: K): void; +declare function f30(x: T, y: Partial): void; +declare function f31(x: T, y: Partial): void; +declare function f40(x: T, y: Readonly): void; +declare function f41(x: T, y: Readonly): void; diff --git a/tests/cases/conformance/types/mapped/mappedTypeRelationships.ts b/tests/cases/conformance/types/mapped/mappedTypeRelationships.ts new file mode 100644 index 00000000000..7bc1c1a1670 --- /dev/null +++ b/tests/cases/conformance/types/mapped/mappedTypeRelationships.ts @@ -0,0 +1,90 @@ +// @strictNullChecks: true +// @declaration: true + +function f1(x: T, k: keyof T) { + return x[k]; +} + +function f2(x: T, k: K) { + return x[k]; +} + +function f3(x: T, y: U, k: keyof T) { + x[k] = y[k]; + y[k] = x[k]; // Error +} + +function f4(x: T, y: U, k: K) { + x[k] = y[k]; + y[k] = x[k]; // Error +} + +function f5(x: T, y: U, k: keyof U) { + x[k] = y[k]; // Error + y[k] = x[k]; // Error +} + +function f6(x: T, y: U, k: K) { + x[k] = y[k]; // Error + y[k] = x[k]; // Error +} + +function f10(x: T, y: Partial, k: keyof T) { + x[k] = y[k]; // Error + y[k] = x[k]; +} + +function f11(x: T, y: Partial, k: K) { + x[k] = y[k]; // Error + y[k] = x[k]; +} + +function f12(x: T, y: Partial, k: keyof T) { + x[k] = y[k]; // Error + y[k] = x[k]; // Error +} + +function f13(x: T, y: Partial, k: K) { + x[k] = y[k]; // Error + y[k] = x[k]; // Error +} + +function f20(x: T, y: Readonly, k: keyof T) { + x[k] = y[k]; + y[k] = x[k]; // Error +} + +function f21(x: T, y: Readonly, k: K) { + x[k] = y[k]; + y[k] = x[k]; // Error +} + +function f22(x: T, y: Readonly, k: keyof T) { + x[k] = y[k]; + y[k] = x[k]; // Error +} + +function f23(x: T, y: Readonly, k: K) { + x[k] = y[k]; + y[k] = x[k]; // Error +} + +function f30(x: T, y: Partial) { + x = y; // Error + y = x; +} + +function f31(x: T, y: Partial) { + x = y; // Error + y = x; +} + +function f40(x: T, y: Readonly) { + x = y; + y = x; +} + +function f41(x: T, y: Readonly) { + x = y; + y = x; +} \ No newline at end of file