TypeScript/tests/baselines/reference/stringIndexerConstrainsPropertyDeclarations.errors.txt

156 lines
5.4 KiB
Plaintext

==== tests/cases/conformance/types/objectTypeLiteral/indexSignatures/stringIndexerConstrainsPropertyDeclarations.ts (27 errors) ====
// String indexer types constrain the types of named properties in their containing type
interface MyString extends String {
foo: number;
}
class C {
[x: string]: string;
constructor() { } // ok
a: string; // ok
b: number; // error
~~~~~~~~~~
!!! Property 'b' of type 'number' is not assignable to string index type 'string'.
c: () => {} // error
~~~~~~~~~~~
!!! Property 'c' of type '() => {}' is not assignable to string index type 'string'.
"d": string; // ok
"e": number; // error
~~~~~~~~~~~~
!!! Property '"e"' of type 'number' is not assignable to string index type 'string'.
1.0: string; // ok
2.0: number; // error
~~~~~~~~~~~~
!!! Property '2.0' of type 'number' is not assignable to string index type 'string'.
"3.0": string; // ok
"4.0": number; // error
~~~~~~~~~~~~~~
!!! Property '"4.0"' of type 'number' is not assignable to string index type 'string'.
f: MyString; // error
~~~~~~~~~~~~
!!! Property 'f' of type 'MyString' is not assignable to string index type 'string'.
get X() { // ok
~
!!! Accessors are only available when targeting ECMAScript 5 and higher.
return '';
}
set X(v) { } // ok
~
!!! Accessors are only available when targeting ECMAScript 5 and higher.
foo() { // error
~~~~~~~~~~~~~~~~
return '';
~~~~~~~~~~~~~~~~~~
}
~~~~~
!!! Property 'foo' of type '() => string' is not assignable to string index type 'string'.
static sa: number; // ok
static sb: string; // ok
static foo() { } // ok
static get X() { // ok
~
!!! Accessors are only available when targeting ECMAScript 5 and higher.
return 1;
}
}
interface I {
[x: string]: string;
a: string; // ok
b: number; // error
~~~~~~~~~~
!!! Property 'b' of type 'number' is not assignable to string index type 'string'.
c: () => {} // error
~~~~~~~~~~~
!!! Property 'c' of type '() => {}' is not assignable to string index type 'string'.
"d": string; // ok
"e": number; // error
~~~~~~~~~~~~
!!! Property '"e"' of type 'number' is not assignable to string index type 'string'.
1.0: string; // ok
2.0: number; // error
~~~~~~~~~~~~
!!! Property '2.0' of type 'number' is not assignable to string index type 'string'.
(): string; // ok
(x): number // ok
foo(): string; // error
~~~~~~~~~~~~~~
!!! Property 'foo' of type '() => string' is not assignable to string index type 'string'.
"3.0": string; // ok
"4.0": number; // error
~~~~~~~~~~~~~~
!!! Property '"4.0"' of type 'number' is not assignable to string index type 'string'.
f: MyString; // error
~~~~~~~~~~~~
!!! Property 'f' of type 'MyString' is not assignable to string index type 'string'.
}
var a: {
[x: string]: string;
a: string; // ok
b: number; // error
~~~~~~~~~~
!!! Property 'b' of type 'number' is not assignable to string index type 'string'.
c: () => {} // error
~~~~~~~~~~~
!!! Property 'c' of type '() => {}' is not assignable to string index type 'string'.
"d": string; // ok
"e": number; // error
~~~~~~~~~~~~
!!! Property '"e"' of type 'number' is not assignable to string index type 'string'.
1.0: string; // ok
2.0: number; // error
~~~~~~~~~~~~
!!! Property '2.0' of type 'number' is not assignable to string index type 'string'.
(): string; // ok
(x): number // ok
foo(): string; // error
~~~~~~~~~~~~~~
!!! Property 'foo' of type '() => string' is not assignable to string index type 'string'.
"3.0": string; // ok
"4.0": number; // error
~~~~~~~~~~~~~~
!!! Property '"4.0"' of type 'number' is not assignable to string index type 'string'.
f: MyString; // error
~~~~~~~~~~~~
!!! Property 'f' of type 'MyString' is not assignable to string index type 'string'.
}
// error
var b: { [x: string]: string; } = {
~
!!! Type '{ [x: string]: {}; 1.0: string; 2.0: number; a: string; b: number; c: () => void; "d": string; "e": number; "3.0": string; "4.0": number; f: MyString; X: string; foo: () => string; }' is not assignable to type '{ [x: string]: string; }':
!!! Index signatures are incompatible:
!!! Type '{}' is not assignable to type 'string'.
a: '',
b: 1,
c: () => { },
"d": '',
"e": 1,
1.0: '',
2.0: 1,
"3.0": '',
"4.0": 1,
f: <MyString>null,
get X() {
~
!!! Accessors are only available when targeting ECMAScript 5 and higher.
return '';
},
set X(v) { },
~
!!! Accessors are only available when targeting ECMAScript 5 and higher.
foo() {
return '';
}
}