1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
|
tests/cases/conformance/types/unknown/unknownType1.ts(50,5): error TS18046: 'x' is of type 'unknown'.
tests/cases/conformance/types/unknown/unknownType1.ts(51,5): error TS18046: 'x' is of type 'unknown'.
tests/cases/conformance/types/unknown/unknownType1.ts(52,5): error TS18046: 'x' is of type 'unknown'.
tests/cases/conformance/types/unknown/unknownType1.ts(53,5): error TS18046: 'x' is of type 'unknown'.
tests/cases/conformance/types/unknown/unknownType1.ts(54,5): error TS18046: 'x' is of type 'unknown'.
tests/cases/conformance/types/unknown/unknownType1.ts(55,5): error TS18046: 'x' is of type 'unknown'.
tests/cases/conformance/types/unknown/unknownType1.ts(56,6): error TS18046: 'x' is of type 'unknown'.
tests/cases/conformance/types/unknown/unknownType1.ts(57,6): error TS18046: 'x' is of type 'unknown'.
tests/cases/conformance/types/unknown/unknownType1.ts(63,5): error TS18046: 'x' is of type 'unknown'.
tests/cases/conformance/types/unknown/unknownType1.ts(64,5): error TS18046: 'x' is of type 'unknown'.
tests/cases/conformance/types/unknown/unknownType1.ts(65,5): error TS18046: 'x' is of type 'unknown'.
tests/cases/conformance/types/unknown/unknownType1.ts(66,9): error TS18046: 'x' 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(120,9): error TS2322: Type 'T' 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(170,9): error TS2322: Type 'T' is not assignable to type '{}'.
tests/cases/conformance/types/unknown/unknownType1.ts(171,9): error TS2322: Type 'U' is not assignable to type '{}'.
tests/cases/conformance/types/unknown/unknownType1.ts(181,5): error TS2322: Type 'T' 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 TS18046: 'x' is of type 'unknown'.
x.foo; // Error
~
!!! error TS18046: 'x' is of type 'unknown'.
x[10]; // Error
~
!!! error TS18046: 'x' is of type 'unknown'.
x(); // Error
~
!!! error TS18046: 'x' is of type 'unknown'.
x + 1; // Error
~
!!! error TS18046: 'x' is of type 'unknown'.
x * 2; // Error
~
!!! error TS18046: 'x' is of type 'unknown'.
-x; // Error
~
!!! error TS18046: 'x' is of type 'unknown'.
+x; // Error
~
!!! error TS18046: 'x' is of type 'unknown'.
}
// No property accesses, element accesses, or function calls
function f11(x: unknown) {
x.foo; // Error
~
!!! error TS18046: 'x' is of type 'unknown'.
x[5]; // Error
~
!!! error TS18046: 'x' is of type 'unknown'.
x(); // Error
~
!!! error TS18046: 'x' is of type 'unknown'.
new x(); // Error
~
!!! error TS18046: 'x' 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
}
// 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'.
}
// 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;
~
!!! error TS2322: Type 'T' is not assignable to type '{}'.
!!! related TS2208 tests/cases/conformance/types/unknown/unknownType1.ts:169:14: This type parameter might need an `extends {}` constraint.
let y: {} = u;
~
!!! error TS2322: Type 'U' 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 '{}'.
}
|