Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
Accept new baselines
  • Loading branch information
ahejlsberg committed Apr 4, 2019
commit e01fa854e26aa95c87dd87609e3b77d23d376f3a
200 changes: 200 additions & 0 deletions tests/baselines/reference/keyofAndIndexedAccess2.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(4,5): error TS2322: Type '"x"' is not assignable to type 'number'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(6,5): error TS2322: Type '2' is not assignable to type '0 | 1'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(7,5): error TS2322: Type '"x"' is not assignable to type '0 | 1'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(8,5): error TS2322: Type '1' is not assignable to type 'never'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(9,5): error TS2322: Type '2' is not assignable to type 'never'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(10,5): error TS2322: Type '"x"' is not assignable to type 'never'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(14,5): error TS2739: Type '{ [key: string]: number; }' is missing the following properties from type '{ x: number; y: number; }': x, y
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(15,5): error TS2322: Type 'T' is not assignable to type '{ x: number; y: number; }'.
Type '{ [key: string]: number; }' is missing the following properties from type '{ x: number; y: number; }': x, y
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(18,5): error TS2322: Type '{ x: number; y: number; }' is not assignable to type 'T'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(19,5): error TS2322: Type '{ [key: string]: number; }' is not assignable to type 'T'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(26,7): error TS2339: Property 'x' does not exist on type 'T'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(27,5): error TS2322: Type '1' is not assignable to type 'T[keyof T]'.
Type '1' is not assignable to type 'T[string] & T[number]'.
Type '1' is not assignable to type 'T[string]'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(31,5): error TS2322: Type '{ [key: string]: number; }' is not assignable to type '{ [P in K]: number; }'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(45,3): error TS7017: Element implicitly has an 'any' type because type 'Item' has no index signature.
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(46,3): error TS2322: Type '123' is not assignable to type 'string & number'.
Type '123' is not assignable to type 'string'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(47,3): error TS2322: Type '123' is not assignable to type 'T[keyof T]'.
Type '123' is not assignable to type 'T["a"] & T["b"]'.
Type '123' is not assignable to type 'T["a"]'.
Type '123' is not assignable to type 'string'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(48,3): error TS2322: Type '123' is not assignable to type 'T[K]'.
Type '123' is not assignable to type 'T["a"] & T["b"]'.
Type '123' is not assignable to type 'T["a"]'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(60,7): error TS2339: Property 'foo' does not exist on type 'T'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(61,3): error TS2536: Type 'string' cannot be used to index type 'T'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(62,3): error TS2322: Type '123' is not assignable to type 'T[keyof T]'.
Type '123' is not assignable to type 'T[string]'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(63,3): error TS2322: Type '123' is not assignable to type 'T[K]'.
Type '123' is not assignable to type 'T[string]'.
tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(103,5): error TS2322: Type '123' is not assignable to type 'Type[K]'.
Type '123' is not assignable to type '123 & "some string"'.
Type '123' is not assignable to type '"some string"'.


==== tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts (22 errors) ====
function f1(obj: { a: number, b: 0 | 1, c: string }, k0: 'a', k1: 'a' | 'b', k2: 'a' | 'b' | 'c') {
obj[k0] = 1;
obj[k0] = 2;
obj[k0] = 'x'; // Error
~~~~~~~
!!! error TS2322: Type '"x"' is not assignable to type 'number'.
obj[k1] = 1;
obj[k1] = 2; // Error
~~~~~~~
!!! error TS2322: Type '2' is not assignable to type '0 | 1'.
obj[k1] = 'x'; // Error
~~~~~~~
!!! error TS2322: Type '"x"' is not assignable to type '0 | 1'.
obj[k2] = 1; // Error
~~~~~~~
!!! error TS2322: Type '1' is not assignable to type 'never'.
obj[k2] = 2; // Error
~~~~~~~
!!! error TS2322: Type '2' is not assignable to type 'never'.
obj[k2] = 'x'; // Error
~~~~~~~
!!! error TS2322: Type '"x"' is not assignable to type 'never'.
}

function f2<T extends { [key: string]: number }>(a: { x: number, y: number }, b: { [key: string]: number }, c: T, k: keyof T) {
a = b; // Error, index signature in source doesn't imply properties are present
~
!!! error TS2739: Type '{ [key: string]: number; }' is missing the following properties from type '{ x: number; y: number; }': x, y
a = c; // Error, index signature in source doesn't imply properties are present
~
!!! error TS2322: Type 'T' is not assignable to type '{ x: number; y: number; }'.
!!! error TS2322: Type '{ [key: string]: number; }' is missing the following properties from type '{ x: number; y: number; }': x, y
b = a;
b = c;
c = a; // Error, constraint on target doesn't imply any properties or signatures
~
!!! error TS2322: Type '{ x: number; y: number; }' is not assignable to type 'T'.
c = b; // Error, constraint on target doesn't imply any properties or signatures
~
!!! error TS2322: Type '{ [key: string]: number; }' is not assignable to type 'T'.
a.x;
b.x;
c.x;
c[k];
a.x = 1;
b.x = 1;
c.x = 1; // Error, cannot write to index signature through constraint
~
!!! error TS2339: Property 'x' does not exist on type 'T'.
c[k] = 1; // Error, cannot write to index signature through constraint
~~~~
!!! error TS2322: Type '1' is not assignable to type 'T[keyof T]'.
!!! error TS2322: Type '1' is not assignable to type 'T[string] & T[number]'.
!!! error TS2322: Type '1' is not assignable to type 'T[string]'.
}

function f3<K extends string>(a: { [P in K]: number }, b: { [key: string]: number }, k: K) {
a = b; // Error, index signature doesn't imply properties are present
~
!!! error TS2322: Type '{ [key: string]: number; }' is not assignable to type '{ [P in K]: number; }'.
b = a;
a[k];
a[k] = 1;
}

function f4<K extends string>(a: { [key: string]: number }[K], b: number) {
a = b;
b = a;
}

type Item = { a: string, b: number };

function f10<T extends Item, K extends keyof T>(obj: T, k1: string, k2: keyof Item, k3: keyof T, k4: K) {
obj[k1] = 123; // Error
~~~~~~~
!!! error TS7017: Element implicitly has an 'any' type because type 'Item' has no index signature.
obj[k2] = 123; // Error
~~~~~~~
!!! error TS2322: Type '123' is not assignable to type 'string & number'.
!!! error TS2322: Type '123' is not assignable to type 'string'.
obj[k3] = 123; // Error
~~~~~~~
!!! error TS2322: Type '123' is not assignable to type 'T[keyof T]'.
!!! error TS2322: Type '123' is not assignable to type 'T["a"] & T["b"]'.
!!! error TS2322: Type '123' is not assignable to type 'T["a"]'.
!!! error TS2322: Type '123' is not assignable to type 'string'.
obj[k4] = 123; // Error
~~~~~~~
!!! error TS2322: Type '123' is not assignable to type 'T[K]'.
!!! error TS2322: Type '123' is not assignable to type 'T["a"] & T["b"]'.
!!! error TS2322: Type '123' is not assignable to type 'T["a"]'.
}

type Dict = Record<string, number>;

function f11<K extends keyof Dict>(obj: Dict, k1: keyof Dict, k2: K) {
obj.foo = 123;
obj[k1] = 123;
obj[k2] = 123;
}

function f12<T extends Readonly<Dict>, K extends keyof T>(obj: T, k1: keyof Dict, k2: keyof T, k3: K) {
obj.foo = 123; // Error
~~~
!!! error TS2339: Property 'foo' does not exist on type 'T'.
obj[k1] = 123; // Error
~~~~~~~
!!! error TS2536: Type 'string' cannot be used to index type 'T'.
obj[k2] = 123; // Error
~~~~~~~
!!! error TS2322: Type '123' is not assignable to type 'T[keyof T]'.
!!! error TS2322: Type '123' is not assignable to type 'T[string]'.
obj[k3] = 123; // Error
~~~~~~~
!!! error TS2322: Type '123' is not assignable to type 'T[K]'.
!!! error TS2322: Type '123' is not assignable to type 'T[string]'.
}

// Repro from #27895

export interface Entity {
id: number | string;
}

export type IdOf<E extends Entity> = E['id'];

export interface EntityState<E extends Entity> {
ids: IdOf<E>[];
entities: { [key: string]: E, [key: number]: E };
}


export function getAllEntities<E extends Entity>(state: EntityState<E>): E[] {
const { ids, entities } = state;
return ids.map(id => entities[id]);
}

export function getEntity<E extends Entity>(id: IdOf<E>, state: EntityState<E>): E | undefined {
const { ids, entities } = state;

if (!ids.includes(id)) {
return undefined;
}

return entities[id];
}

// Repro from #30603

interface Type {
a: 123;
b: "some string";
}

function get123<K extends keyof Type>(): Type[K] {
return 123; // Error
~~~~~~~~~~~
!!! error TS2322: Type '123' is not assignable to type 'Type[K]'.
!!! error TS2322: Type '123' is not assignable to type '123 & "some string"'.
!!! error TS2322: Type '123' is not assignable to type '"some string"'.
}

176 changes: 176 additions & 0 deletions tests/baselines/reference/keyofAndIndexedAccess2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
//// [keyofAndIndexedAccess2.ts]
function f1(obj: { a: number, b: 0 | 1, c: string }, k0: 'a', k1: 'a' | 'b', k2: 'a' | 'b' | 'c') {
obj[k0] = 1;
obj[k0] = 2;
obj[k0] = 'x'; // Error
obj[k1] = 1;
obj[k1] = 2; // Error
obj[k1] = 'x'; // Error
obj[k2] = 1; // Error
obj[k2] = 2; // Error
obj[k2] = 'x'; // Error
}

function f2<T extends { [key: string]: number }>(a: { x: number, y: number }, b: { [key: string]: number }, c: T, k: keyof T) {
a = b; // Error, index signature in source doesn't imply properties are present
a = c; // Error, index signature in source doesn't imply properties are present
b = a;
b = c;
c = a; // Error, constraint on target doesn't imply any properties or signatures
c = b; // Error, constraint on target doesn't imply any properties or signatures
a.x;
b.x;
c.x;
c[k];
a.x = 1;
b.x = 1;
c.x = 1; // Error, cannot write to index signature through constraint
c[k] = 1; // Error, cannot write to index signature through constraint
}

function f3<K extends string>(a: { [P in K]: number }, b: { [key: string]: number }, k: K) {
a = b; // Error, index signature doesn't imply properties are present
b = a;
a[k];
a[k] = 1;
}

function f4<K extends string>(a: { [key: string]: number }[K], b: number) {
a = b;
b = a;
}

type Item = { a: string, b: number };

function f10<T extends Item, K extends keyof T>(obj: T, k1: string, k2: keyof Item, k3: keyof T, k4: K) {
obj[k1] = 123; // Error
obj[k2] = 123; // Error
obj[k3] = 123; // Error
obj[k4] = 123; // Error
}

type Dict = Record<string, number>;

function f11<K extends keyof Dict>(obj: Dict, k1: keyof Dict, k2: K) {
obj.foo = 123;
obj[k1] = 123;
obj[k2] = 123;
}

function f12<T extends Readonly<Dict>, K extends keyof T>(obj: T, k1: keyof Dict, k2: keyof T, k3: K) {
obj.foo = 123; // Error
obj[k1] = 123; // Error
obj[k2] = 123; // Error
obj[k3] = 123; // Error
}

// Repro from #27895

export interface Entity {
id: number | string;
}

export type IdOf<E extends Entity> = E['id'];

export interface EntityState<E extends Entity> {
ids: IdOf<E>[];
entities: { [key: string]: E, [key: number]: E };
}


export function getAllEntities<E extends Entity>(state: EntityState<E>): E[] {
const { ids, entities } = state;
return ids.map(id => entities[id]);
}

export function getEntity<E extends Entity>(id: IdOf<E>, state: EntityState<E>): E | undefined {
const { ids, entities } = state;

if (!ids.includes(id)) {
return undefined;
}

return entities[id];
}

// Repro from #30603

interface Type {
a: 123;
b: "some string";
}

function get123<K extends keyof Type>(): Type[K] {
return 123; // Error
}


//// [keyofAndIndexedAccess2.js]
function f1(obj, k0, k1, k2) {
obj[k0] = 1;
obj[k0] = 2;
obj[k0] = 'x'; // Error
obj[k1] = 1;
obj[k1] = 2; // Error
obj[k1] = 'x'; // Error
obj[k2] = 1; // Error
obj[k2] = 2; // Error
obj[k2] = 'x'; // Error
}
function f2(a, b, c, k) {
a = b; // Error, index signature in source doesn't imply properties are present
a = c; // Error, index signature in source doesn't imply properties are present
b = a;
b = c;
c = a; // Error, constraint on target doesn't imply any properties or signatures
c = b; // Error, constraint on target doesn't imply any properties or signatures
a.x;
b.x;
c.x;
c[k];
a.x = 1;
b.x = 1;
c.x = 1; // Error, cannot write to index signature through constraint
c[k] = 1; // Error, cannot write to index signature through constraint
}
function f3(a, b, k) {
a = b; // Error, index signature doesn't imply properties are present
b = a;
a[k];
a[k] = 1;
}
function f4(a, b) {
a = b;
b = a;
}
function f10(obj, k1, k2, k3, k4) {
obj[k1] = 123; // Error
obj[k2] = 123; // Error
obj[k3] = 123; // Error
obj[k4] = 123; // Error
}
function f11(obj, k1, k2) {
obj.foo = 123;
obj[k1] = 123;
obj[k2] = 123;
}
function f12(obj, k1, k2, k3) {
obj.foo = 123; // Error
obj[k1] = 123; // Error
obj[k2] = 123; // Error
obj[k3] = 123; // Error
}
export function getAllEntities(state) {
const { ids, entities } = state;
return ids.map(id => entities[id]);
}
export function getEntity(id, state) {
const { ids, entities } = state;
if (!ids.includes(id)) {
return undefined;
}
return entities[id];
}
function get123() {
return 123; // Error
}
Loading