TypeScript/tests/baselines/reference/unknownType1.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

277 lines
11 KiB
Plaintext

tests/cases/conformance/types/unknown/unknownType1.ts(50,5): error TS2571: Object is of type 'unknown'.
tests/cases/conformance/types/unknown/unknownType1.ts(51,5): error TS2571: Object is of type 'unknown'.
tests/cases/conformance/types/unknown/unknownType1.ts(52,5): error TS2571: Object is of type 'unknown'.
tests/cases/conformance/types/unknown/unknownType1.ts(53,5): error TS2571: Object is of type 'unknown'.
tests/cases/conformance/types/unknown/unknownType1.ts(54,5): error TS2571: Object is of type 'unknown'.
tests/cases/conformance/types/unknown/unknownType1.ts(55,5): error TS2571: Object is of type 'unknown'.
tests/cases/conformance/types/unknown/unknownType1.ts(56,6): error TS2571: Object is of type 'unknown'.
tests/cases/conformance/types/unknown/unknownType1.ts(57,6): error TS2571: Object is of type 'unknown'.
tests/cases/conformance/types/unknown/unknownType1.ts(63,5): error TS2571: Object is of type 'unknown'.
tests/cases/conformance/types/unknown/unknownType1.ts(64,5): error TS2571: Object is of type 'unknown'.
tests/cases/conformance/types/unknown/unknownType1.ts(65,5): error TS2571: Object is of type 'unknown'.
tests/cases/conformance/types/unknown/unknownType1.ts(66,9): error TS2571: Object is of type 'unknown'.
tests/cases/conformance/types/unknown/unknownType1.ts(110,9): error TS2322: Type 'unknown' is not assignable to type 'object'.
tests/cases/conformance/types/unknown/unknownType1.ts(111,9): error TS2322: Type 'unknown' is not assignable to type 'string'.
tests/cases/conformance/types/unknown/unknownType1.ts(112,9): error TS2322: Type 'unknown' is not assignable to type 'string[]'.
tests/cases/conformance/types/unknown/unknownType1.ts(113,9): error TS2322: Type 'unknown' is not assignable to type '{}'.
tests/cases/conformance/types/unknown/unknownType1.ts(114,9): error TS2322: Type 'unknown' is not assignable to type '{} | null | undefined'.
Type 'unknown' is not assignable to type '{}'.
tests/cases/conformance/types/unknown/unknownType1.ts(120,9): error TS2322: Type 'T' is not assignable to type 'object'.
Type 'unknown' is not assignable to type 'object'.
tests/cases/conformance/types/unknown/unknownType1.ts(128,5): error TS2322: Type 'number[]' is not assignable to type '{ [x: string]: unknown; }'.
Index signature for type 'string' is missing in type 'number[]'.
tests/cases/conformance/types/unknown/unknownType1.ts(129,5): error TS2322: Type 'number' is not assignable to type '{ [x: string]: unknown; }'.
tests/cases/conformance/types/unknown/unknownType1.ts(143,29): error TS2698: Spread types may only be created from object types.
tests/cases/conformance/types/unknown/unknownType1.ts(144,29): error TS2698: Spread types may only be created from object types.
tests/cases/conformance/types/unknown/unknownType1.ts(150,17): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
tests/cases/conformance/types/unknown/unknownType1.ts(156,14): error TS2700: Rest types may only be created from object types.
tests/cases/conformance/types/unknown/unknownType1.ts(162,5): error TS2564: Property 'a' has no initializer and is not definitely assigned in the constructor.
tests/cases/conformance/types/unknown/unknownType1.ts(171,9): error TS2322: Type 'U' is not assignable to type '{}'.
Type 'unknown' is not assignable to type '{}'.
tests/cases/conformance/types/unknown/unknownType1.ts(181,5): error TS2322: Type 'T' is not assignable to type '{}'.
Type 'unknown' is not assignable to type '{}'.
==== tests/cases/conformance/types/unknown/unknownType1.ts (27 errors) ====
// In an intersection everything absorbs unknown
type T00 = unknown & null; // null
type T01 = unknown & undefined; // undefined
type T02 = unknown & null & undefined; // never
type T03 = unknown & string; // string
type T04 = unknown & string[]; // string[]
type T05 = unknown & unknown; // unknown
type T06 = unknown & any; // any
// In a union an unknown absorbs everything
type T10 = unknown | null; // unknown
type T11 = unknown | undefined; // unknown
type T12 = unknown | null | undefined; // unknown
type T13 = unknown | string; // unknown
type T14 = unknown | string[]; // unknown
type T15 = unknown | unknown; // unknown
type T16 = unknown | any; // any
// Type variable and unknown in union and intersection
type T20<T> = T & {}; // T & {}
type T21<T> = T | {}; // T | {}
type T22<T> = T & unknown; // T
type T23<T> = T | unknown; // unknown
// unknown in conditional types
type T30<T> = unknown extends T ? true : false; // Deferred
type T31<T> = T extends unknown ? true : false; // Deferred (so it distributes)
type T32<T> = never extends T ? true : false; // true
type T33<T> = T extends never ? true : false; // Deferred
type T35<T> = T extends unknown ? { x: T } : false;
type T36 = T35<string | number>; // { x: string } | { x: number }
type T37 = T35<any>; // { x: any }
type T38 = T35<unknown>; // { x: unknown }
// keyof unknown
type T40 = keyof any; // string | number | symbol
type T41 = keyof unknown; // never
// Only equality operators are allowed with unknown
function f10(x: unknown) {
x == 5;
x !== 10;
x >= 0; // Error
~
!!! error TS2571: Object is of type 'unknown'.
x.foo; // Error
~
!!! error TS2571: Object is of type 'unknown'.
x[10]; // Error
~
!!! error TS2571: Object is of type 'unknown'.
x(); // Error
~
!!! error TS2571: Object is of type 'unknown'.
x + 1; // Error
~
!!! error TS2571: Object is of type 'unknown'.
x * 2; // Error
~
!!! error TS2571: Object is of type 'unknown'.
-x; // Error
~
!!! error TS2571: Object is of type 'unknown'.
+x; // Error
~
!!! error TS2571: Object is of type 'unknown'.
}
// No property accesses, element accesses, or function calls
function f11(x: unknown) {
x.foo; // Error
~
!!! error TS2571: Object is of type 'unknown'.
x[5]; // Error
~
!!! error TS2571: Object is of type 'unknown'.
x(); // Error
~
!!! error TS2571: Object is of type 'unknown'.
new x(); // Error
~
!!! error TS2571: Object is of type 'unknown'.
}
// typeof, instanceof, and user defined type predicates
declare function isFunction(x: unknown): x is Function;
function f20(x: unknown) {
if (typeof x === "string" || typeof x === "number") {
x; // string | number
}
if (x instanceof Error) {
x; // Error
}
if (isFunction(x)) {
x; // Function
}
}
// Homomorphic mapped type over unknown
type T50<T> = { [P in keyof T]: number };
type T51 = T50<any>; // { [x: string]: number }
type T52 = T50<unknown>; // {}
// Anything is assignable to unknown
function f21<T>(pAny: any, pNever: never, pT: T) {
let x: unknown;
x = 123;
x = "hello";
x = [1, 2, 3];
x = new Error();
x = x;
x = pAny;
x = pNever;
x = pT;
}
// unknown assignable only to itself and any
function f22(x: unknown) {
let v1: any = x;
let v2: unknown = x;
let v3: object = x; // Error
~~
!!! error TS2322: Type 'unknown' is not assignable to type 'object'.
let v4: string = x; // Error
~~
!!! error TS2322: Type 'unknown' is not assignable to type 'string'.
let v5: string[] = x; // Error
~~
!!! error TS2322: Type 'unknown' is not assignable to type 'string[]'.
let v6: {} = x; // Error
~~
!!! error TS2322: Type 'unknown' is not assignable to type '{}'.
let v7: {} | null | undefined = x; // Error
~~
!!! error TS2322: Type 'unknown' is not assignable to type '{} | null | undefined'.
!!! error TS2322: Type 'unknown' is not assignable to type '{}'.
}
// Type parameter 'T extends unknown' not related to object
function f23<T extends unknown>(x: T) {
let y: object = x; // Error
~
!!! error TS2322: Type 'T' is not assignable to type 'object'.
!!! error TS2322: Type 'unknown' is not assignable to type 'object'.
}
// Anything fresh but primitive assignable to { [x: string]: unknown }
function f24(x: { [x: string]: unknown }) {
x = {};
x = { a: 5 };
x = [1, 2, 3]; // Error
~
!!! error TS2322: Type 'number[]' is not assignable to type '{ [x: string]: unknown; }'.
!!! error TS2322: Index signature for type 'string' is missing in type 'number[]'.
x = 123; // Error
~
!!! error TS2322: Type 'number' is not assignable to type '{ [x: string]: unknown; }'.
}
// Locals of type unknown always considered initialized
function f25() {
let x: unknown;
let y = x;
}
// Spread of unknown causes result to be unknown
function f26(x: {}, y: unknown, z: any) {
let o1 = { a: 42, ...x }; // { a: number }
let o2 = { a: 42, ...x, ...y }; // unknown
~~~~
!!! error TS2698: Spread types may only be created from object types.
let o3 = { a: 42, ...x, ...y, ...z }; // any
~~~~
!!! error TS2698: Spread types may only be created from object types.
let o4 = { a: 42, ...z }; // any
}
// Functions with unknown return type don't need return expressions
function f27(): unknown {
~~~~~~~
!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
}
// Rest type cannot be created from unknown
function f28(x: unknown) {
let { ...a } = x; // Error
~
!!! error TS2700: Rest types may only be created from object types.
}
// Class properties of type unknown don't need definite assignment
class C1 {
a: string; // Error
~
!!! error TS2564: Property 'a' has no initializer and is not definitely assigned in the constructor.
b: unknown;
c: any;
}
// Type parameter with explicit 'unknown' constraint not assignable to '{}'
function f30<T, U extends unknown>(t: T, u: U) {
let x: {} = t;
let y: {} = u;
~
!!! error TS2322: Type 'U' is not assignable to type '{}'.
!!! error TS2322: Type 'unknown' is not assignable to type '{}'.
}
// Repro from #26796
type Test1 = [unknown] extends [{}] ? true : false; // false
type IsDefinitelyDefined<T extends unknown> = [T] extends [{}] ? true : false;
type Test2 = IsDefinitelyDefined<unknown>; // false
function oops<T extends unknown>(arg: T): {} {
return arg; // Error
~~~~~~~~~~~
!!! error TS2322: Type 'T' is not assignable to type '{}'.
!!! error TS2322: Type 'unknown' is not assignable to type '{}'.
}