57 lines
1.9 KiB
Plaintext
57 lines
1.9 KiB
Plaintext
==== tests/cases/conformance/types/objectTypeLiteral/indexSignatures/stringIndexerConstrainsPropertyDeclarations2.ts (7 errors) ====
|
|
// String indexer providing a constraint of a user defined type
|
|
|
|
class A {
|
|
foo(): string { return ''; }
|
|
}
|
|
|
|
class B extends A {
|
|
bar(): string { return ''; }
|
|
}
|
|
|
|
class Foo {
|
|
[x: string]: A;
|
|
a: A; // ok
|
|
b: B; // ok
|
|
c: number; // error
|
|
~~~~~~~~~~
|
|
!!! error TS2411: Property 'c' of type 'number' is not assignable to string index type 'A'.
|
|
d: string; // error
|
|
~~~~~~~~~~
|
|
!!! error TS2411: Property 'd' of type 'string' is not assignable to string index type 'A'.
|
|
}
|
|
|
|
interface Foo2 {
|
|
[x: string]: A;
|
|
a: A; // ok
|
|
b: B; // ok
|
|
c: number; // error
|
|
~~~~~~~~~~
|
|
!!! error TS2411: Property 'c' of type 'number' is not assignable to string index type 'A'.
|
|
d: string; // error
|
|
~~~~~~~~~~
|
|
!!! error TS2411: Property 'd' of type 'string' is not assignable to string index type 'A'.
|
|
}
|
|
|
|
var a: {
|
|
[x: string]: A;
|
|
a: A; // ok
|
|
b: B; // ok
|
|
c: number; // error
|
|
~~~~~~~~~~
|
|
!!! error TS2411: Property 'c' of type 'number' is not assignable to string index type 'A'.
|
|
d: string; // error
|
|
~~~~~~~~~~
|
|
!!! error TS2411: Property 'd' of type 'string' is not assignable to string index type 'A'.
|
|
};
|
|
|
|
// error
|
|
var b: { [x: string]: A } = {
|
|
~
|
|
!!! error TS2322: Type '{ [x: string]: typeof A; a: typeof A; b: typeof B; }' is not assignable to type '{ [x: string]: A; }':
|
|
!!! error TS2322: Index signatures are incompatible:
|
|
!!! error TS2322: Type 'typeof A' is not assignable to type 'A':
|
|
!!! error TS2322: Property 'foo' is missing in type 'typeof A'.
|
|
a: A,
|
|
b: B
|
|
} |