TypeScript/tests/baselines/reference/objectTypeWithStringAndNumberIndexSignatureToAny.errors.txt
Anders Hejlsberg 0e905be42b
Index signatures for symbols and template literal strings (#44512)
* Switch index signature storage to 'indexInfos: IndexInfo[]' property

* Accept new baselines

* Remove another usage of IndexKind enum

* Update getIndexedAccessType and resolveMappedTypeMembers

* Accept new baselines

* Update grammar checking for index signatures

* Accept new baselines

* Consider all index signatures in mapped types and union types

* Accept new baselines

* Update getIndexType

* Accept new baselines

* Intersect multiple applicable index signatures

* Use getApplicableIndexInfo instead of hardwired string/number handling

* Update index signature relationship checking

* Report type for which index signature is missing

* Report type for which index signature is missing

* Accept new baselines

* Make 'number' index signatures consistently apply to numeric strings

* Accept new baselines

* Update fourslash test

* Revise index constraint checking

* Accept new baselines

* Update error messages

* Accept new baselines

* Update type inference from index signatures

* Update isKnownProperty

* Update contextual typing based on index signatures

* Accept new baselines

* Support union types in index signature declarations

* Accept new baselines

* Check duplicate index signatures / remove redundant template literals from unions with string

* Accept new baselines

* Include key type in diagnostic / check symbol-named properties

* Accept new baselines

* Minor fix

* Add tests

* Accept new baselines

* Add optimized findApplicableIndexInfoForName

* Accept new baselines

* Another place we don't need to obtain literal type for property name

* Accept new baselines

* Don't create literal types that are going to be discarded

* Individual maps for string, number, bigint, and enum literal types

* Remove ineffective optimizations

* Accept new baselines

* Permit intersections as key types in index signatures

* Index expression in element access is template literal context

* Add tests

* Accept new baselines

* Symbol index signatures from object literals with computed symbol properties

* Accept new baselines

* Add more tests

* Accept new baselines

* Implement Go To Definition for all applicable index signatures

* Add fourslash test

* Accept new API baselines
2021-06-21 11:25:42 -07:00

145 lines
7 KiB
Plaintext

tests/cases/conformance/types/members/objectTypeWithStringAndNumberIndexSignatureToAny.ts(43,5): error TS2322: Type 'Obj' is not assignable to type 'NumberTo<any>'.
Index signature for type 'number' is missing in type 'Obj'.
tests/cases/conformance/types/members/objectTypeWithStringAndNumberIndexSignatureToAny.ts(49,5): error TS2739: Type 'StringTo<any>' is missing the following properties from type 'Obj': hello, world
tests/cases/conformance/types/members/objectTypeWithStringAndNumberIndexSignatureToAny.ts(50,5): error TS2739: Type 'NumberTo<any>' is missing the following properties from type 'Obj': hello, world
tests/cases/conformance/types/members/objectTypeWithStringAndNumberIndexSignatureToAny.ts(51,5): error TS2739: Type 'StringAndNumberTo<any>' is missing the following properties from type 'Obj': hello, world
tests/cases/conformance/types/members/objectTypeWithStringAndNumberIndexSignatureToAny.ts(61,5): error TS2322: Type 'Obj' is not assignable to type 'NumberTo<any>'.
tests/cases/conformance/types/members/objectTypeWithStringAndNumberIndexSignatureToAny.ts(65,5): error TS2322: Type 'Obj' is not assignable to type 'StringTo<any> & NumberTo<any>'.
Type 'Obj' is not assignable to type 'NumberTo<any>'.
Index signature for type 'number' is missing in type 'Obj'.
tests/cases/conformance/types/members/objectTypeWithStringAndNumberIndexSignatureToAny.ts(67,5): error TS2322: Type 'StringTo<any>' is not assignable to type 'Obj'.
tests/cases/conformance/types/members/objectTypeWithStringAndNumberIndexSignatureToAny.ts(68,5): error TS2322: Type 'NumberTo<any>' is not assignable to type 'Obj'.
tests/cases/conformance/types/members/objectTypeWithStringAndNumberIndexSignatureToAny.ts(69,5): error TS2739: Type 'StringTo<any> & NumberTo<any>' is missing the following properties from type 'Obj': hello, world
tests/cases/conformance/types/members/objectTypeWithStringAndNumberIndexSignatureToAny.ts(84,5): error TS2322: Type 'Obj' is not assignable to type 'NumberToNumber'.
Index signature for type 'number' is missing in type 'Obj'.
tests/cases/conformance/types/members/objectTypeWithStringAndNumberIndexSignatureToAny.ts(88,5): error TS2322: Type 'Obj' is not assignable to type 'StringToAnyNumberToNumber'.
Index signature for type 'number' is missing in type 'Obj'.
tests/cases/conformance/types/members/objectTypeWithStringAndNumberIndexSignatureToAny.ts(90,5): error TS2322: Type 'StringTo<any>' is not assignable to type 'Obj'.
tests/cases/conformance/types/members/objectTypeWithStringAndNumberIndexSignatureToAny.ts(91,5): error TS2739: Type 'NumberTo<number>' is missing the following properties from type 'Obj': hello, world
==== tests/cases/conformance/types/members/objectTypeWithStringAndNumberIndexSignatureToAny.ts (13 errors) ====
// When checking compatibility between two types,
// TypeScript should not require an index signature if
// the target side index signature maps to `any` *and*
// the target side has *any* string index signature to `any`.
//
// So an index signature like in
//
// { [x: number]: any }
//
// is still required of a source type, but neither index signature in
//
// { [x: number]: any, [x: string]: any; }
//
// should be required; *however*, the number index signature in
//
// { [x: number]: number, [x: string]: any; }
//
// should always be required.
interface StringTo<T> {
[x: string]: T;
}
interface NumberTo<T> {
[x: number]: T;
}
interface StringAndNumberTo<T> extends StringTo<T>, NumberTo<T> {
}
interface Obj {
hello: string;
world: number;
}
function f1(sToAny: StringTo<any>, nToAny: NumberTo<any>, bothToAny: StringAndNumberTo<any>, someObj: Obj) {
sToAny = nToAny;
sToAny = bothToAny;
sToAny = someObj;
nToAny = sToAny;
nToAny = bothToAny;
nToAny = someObj;
~~~~~~
!!! error TS2322: Type 'Obj' is not assignable to type 'NumberTo<any>'.
!!! error TS2322: Index signature for type 'number' is missing in type 'Obj'.
bothToAny = sToAny;
bothToAny = nToAny;
bothToAny = someObj;
someObj = sToAny;
~~~~~~~
!!! error TS2739: Type 'StringTo<any>' is missing the following properties from type 'Obj': hello, world
someObj = nToAny;
~~~~~~~
!!! error TS2739: Type 'NumberTo<any>' is missing the following properties from type 'Obj': hello, world
someObj = bothToAny;
~~~~~~~
!!! error TS2739: Type 'StringAndNumberTo<any>' is missing the following properties from type 'Obj': hello, world
}
function f2(sToAny: StringTo<any>, nToAny: NumberTo<any>, bothToAny: StringTo<any> & NumberTo<any>, someObj: Obj) {
sToAny = nToAny;
sToAny = bothToAny;
sToAny = someObj;
nToAny = sToAny;
nToAny = bothToAny;
nToAny = someObj;
~~~~~~
!!! error TS2322: Type 'Obj' is not assignable to type 'NumberTo<any>'.
bothToAny = sToAny;
bothToAny = nToAny;
bothToAny = someObj;
~~~~~~~~~
!!! error TS2322: Type 'Obj' is not assignable to type 'StringTo<any> & NumberTo<any>'.
!!! error TS2322: Type 'Obj' is not assignable to type 'NumberTo<any>'.
!!! error TS2322: Index signature for type 'number' is missing in type 'Obj'.
someObj = sToAny;
~~~~~~~
!!! error TS2322: Type 'StringTo<any>' is not assignable to type 'Obj'.
someObj = nToAny;
~~~~~~~
!!! error TS2322: Type 'NumberTo<any>' is not assignable to type 'Obj'.
someObj = bothToAny;
~~~~~~~
!!! error TS2739: Type 'StringTo<any> & NumberTo<any>' is missing the following properties from type 'Obj': hello, world
}
type NumberToNumber = NumberTo<number>;
interface StringToAnyNumberToNumber extends StringTo<any>, NumberToNumber {
}
function f3(sToAny: StringTo<any>, nToNumber: NumberToNumber, strToAnyNumToNum: StringToAnyNumberToNumber, someObj: Obj) {
sToAny = nToNumber;
sToAny = strToAnyNumToNum;
sToAny = someObj;
nToNumber = sToAny;
nToNumber = strToAnyNumToNum;
nToNumber = someObj;
~~~~~~~~~
!!! error TS2322: Type 'Obj' is not assignable to type 'NumberToNumber'.
!!! error TS2322: Index signature for type 'number' is missing in type 'Obj'.
strToAnyNumToNum = sToAny;
strToAnyNumToNum = nToNumber;
strToAnyNumToNum = someObj;
~~~~~~~~~~~~~~~~
!!! error TS2322: Type 'Obj' is not assignable to type 'StringToAnyNumberToNumber'.
!!! error TS2322: Index signature for type 'number' is missing in type 'Obj'.
someObj = sToAny;
~~~~~~~
!!! error TS2322: Type 'StringTo<any>' is not assignable to type 'Obj'.
someObj = nToNumber;
~~~~~~~
!!! error TS2739: Type 'NumberTo<number>' is missing the following properties from type 'Obj': hello, world
someObj = someObj;
}