Accept new baselines
This commit is contained in:
parent
73af0adcf0
commit
5da063f9c1
165
tests/baselines/reference/unknownType1.errors.txt
Normal file
165
tests/baselines/reference/unknownType1.errors.txt
Normal file
|
@ -0,0 +1,165 @@
|
|||
tests/cases/conformance/types/unknown/unknownType1.ts(87,9): error TS2322: Type 'unknown' is not assignable to type 'object'.
|
||||
tests/cases/conformance/types/unknown/unknownType1.ts(88,9): error TS2322: Type 'unknown' is not assignable to type 'string'.
|
||||
tests/cases/conformance/types/unknown/unknownType1.ts(89,9): error TS2322: Type 'unknown' is not assignable to type 'string[]'.
|
||||
tests/cases/conformance/types/unknown/unknownType1.ts(90,9): error TS2322: Type 'unknown' is not assignable to type '{}'.
|
||||
tests/cases/conformance/types/unknown/unknownType1.ts(91,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(97,9): error TS2322: Type 'T' is not assignable to type 'object'.
|
||||
tests/cases/conformance/types/unknown/unknownType1.ts(106,5): error TS2322: Type '123' is not assignable to type '{ [x: string]: unknown; }'.
|
||||
tests/cases/conformance/types/unknown/unknownType1.ts(132,5): error TS2564: Property 'a' has no initializer and is not definitely assigned in the constructor.
|
||||
|
||||
|
||||
==== tests/cases/conformance/types/unknown/unknownType1.ts (8 errors) ====
|
||||
// In an intersection everything absorbs unknown
|
||||
|
||||
type T00 = unknown & null; // null
|
||||
type T01 = unknown & undefined; // undefined
|
||||
type T02 = unknown & null & undefined; // null & undefined (which becomes never in union)
|
||||
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; // true
|
||||
type T32<T> = never extends T ? true : false; // true
|
||||
type T33<T> = T extends never ? true : false; // Deferred
|
||||
|
||||
// keyof unknown
|
||||
|
||||
type T40 = keyof any; // string | number | symbol
|
||||
type T41 = keyof unknown; // string | number | symbol
|
||||
|
||||
// typeof, instanceof, and user defined type predicates
|
||||
|
||||
declare function isFunction(x: unknown): x is Function;
|
||||
|
||||
function f1(x: unknown) {
|
||||
if (typeof x === "string" || typeof x === "number") {
|
||||
x; // string | number
|
||||
}
|
||||
if (typeof x === "object") {
|
||||
x; // object
|
||||
}
|
||||
if (typeof x === "function") {
|
||||
x; // object
|
||||
}
|
||||
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<unknown>; // { [x: string]: number }
|
||||
type T52 = T50<any>; // { [x: string]: number }
|
||||
|
||||
// Anything is assignable to unknown
|
||||
|
||||
function f2<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 f3(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 f4<T extends unknown>(x: T) {
|
||||
let y: object = x; // Error
|
||||
~
|
||||
!!! error TS2322: Type 'T' is not assignable to type 'object'.
|
||||
}
|
||||
|
||||
// Anything but primitive assignable to { [x: string]: unknown }
|
||||
|
||||
function f5(x: { [x: string]: unknown }) {
|
||||
x = {};
|
||||
x = { a: 5 };
|
||||
x = [1, 2, 3];
|
||||
x = 123; // Error
|
||||
~
|
||||
!!! error TS2322: Type '123' is not assignable to type '{ [x: string]: unknown; }'.
|
||||
}
|
||||
|
||||
// Locals of type unknown always considered initialized
|
||||
|
||||
function f6() {
|
||||
let x: unknown;
|
||||
let y = x;
|
||||
}
|
||||
|
||||
// Spread of unknown causes result to be unknown
|
||||
|
||||
function f7(x: {}, y: unknown, z: any) {
|
||||
let o1 = { a: 42, ...x }; // { a: number }
|
||||
let o2 = { a: 42, ...x, ...y }; // unknown
|
||||
let o3 = { a: 42, ...x, ...y, ...z }; // any
|
||||
}
|
||||
|
||||
// Functions with unknown return type don't need return expressions
|
||||
|
||||
function f8(): unknown {
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
219
tests/baselines/reference/unknownType1.js
Normal file
219
tests/baselines/reference/unknownType1.js
Normal file
|
@ -0,0 +1,219 @@
|
|||
//// [unknownType1.ts]
|
||||
// In an intersection everything absorbs unknown
|
||||
|
||||
type T00 = unknown & null; // null
|
||||
type T01 = unknown & undefined; // undefined
|
||||
type T02 = unknown & null & undefined; // null & undefined (which becomes never in union)
|
||||
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; // true
|
||||
type T32<T> = never extends T ? true : false; // true
|
||||
type T33<T> = T extends never ? true : false; // Deferred
|
||||
|
||||
// keyof unknown
|
||||
|
||||
type T40 = keyof any; // string | number | symbol
|
||||
type T41 = keyof unknown; // string | number | symbol
|
||||
|
||||
// typeof, instanceof, and user defined type predicates
|
||||
|
||||
declare function isFunction(x: unknown): x is Function;
|
||||
|
||||
function f1(x: unknown) {
|
||||
if (typeof x === "string" || typeof x === "number") {
|
||||
x; // string | number
|
||||
}
|
||||
if (typeof x === "object") {
|
||||
x; // object
|
||||
}
|
||||
if (typeof x === "function") {
|
||||
x; // object
|
||||
}
|
||||
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<unknown>; // { [x: string]: number }
|
||||
type T52 = T50<any>; // { [x: string]: number }
|
||||
|
||||
// Anything is assignable to unknown
|
||||
|
||||
function f2<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 f3(x: unknown) {
|
||||
let v1: any = x;
|
||||
let v2: unknown = x;
|
||||
let v3: object = x; // Error
|
||||
let v4: string = x; // Error
|
||||
let v5: string[] = x; // Error
|
||||
let v6: {} = x; // Error
|
||||
let v7: {} | null | undefined = x; // Error
|
||||
}
|
||||
|
||||
// Type parameter 'T extends unknown' not related to object
|
||||
|
||||
function f4<T extends unknown>(x: T) {
|
||||
let y: object = x; // Error
|
||||
}
|
||||
|
||||
// Anything but primitive assignable to { [x: string]: unknown }
|
||||
|
||||
function f5(x: { [x: string]: unknown }) {
|
||||
x = {};
|
||||
x = { a: 5 };
|
||||
x = [1, 2, 3];
|
||||
x = 123; // Error
|
||||
}
|
||||
|
||||
// Locals of type unknown always considered initialized
|
||||
|
||||
function f6() {
|
||||
let x: unknown;
|
||||
let y = x;
|
||||
}
|
||||
|
||||
// Spread of unknown causes result to be unknown
|
||||
|
||||
function f7(x: {}, y: unknown, z: any) {
|
||||
let o1 = { a: 42, ...x }; // { a: number }
|
||||
let o2 = { a: 42, ...x, ...y }; // unknown
|
||||
let o3 = { a: 42, ...x, ...y, ...z }; // any
|
||||
}
|
||||
|
||||
// Functions with unknown return type don't need return expressions
|
||||
|
||||
function f8(): unknown {
|
||||
}
|
||||
|
||||
// Class properties of type unknown don't need definite assignment
|
||||
|
||||
class C1 {
|
||||
a: string; // Error
|
||||
b: unknown;
|
||||
c: any;
|
||||
}
|
||||
|
||||
|
||||
//// [unknownType1.js]
|
||||
"use strict";
|
||||
// In an intersection everything absorbs unknown
|
||||
var __assign = (this && this.__assign) || Object.assign || function(t) {
|
||||
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
||||
s = arguments[i];
|
||||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
||||
t[p] = s[p];
|
||||
}
|
||||
return t;
|
||||
};
|
||||
function f1(x) {
|
||||
if (typeof x === "string" || typeof x === "number") {
|
||||
x; // string | number
|
||||
}
|
||||
if (typeof x === "object") {
|
||||
x; // object
|
||||
}
|
||||
if (typeof x === "function") {
|
||||
x; // object
|
||||
}
|
||||
if (x instanceof Error) {
|
||||
x; // Error
|
||||
}
|
||||
if (isFunction(x)) {
|
||||
x; // Function
|
||||
}
|
||||
}
|
||||
// Anything is assignable to unknown
|
||||
function f2(pAny, pNever, pT) {
|
||||
var x;
|
||||
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 f3(x) {
|
||||
var v1 = x;
|
||||
var v2 = x;
|
||||
var v3 = x; // Error
|
||||
var v4 = x; // Error
|
||||
var v5 = x; // Error
|
||||
var v6 = x; // Error
|
||||
var v7 = x; // Error
|
||||
}
|
||||
// Type parameter 'T extends unknown' not related to object
|
||||
function f4(x) {
|
||||
var y = x; // Error
|
||||
}
|
||||
// Anything but primitive assignable to { [x: string]: unknown }
|
||||
function f5(x) {
|
||||
x = {};
|
||||
x = { a: 5 };
|
||||
x = [1, 2, 3];
|
||||
x = 123; // Error
|
||||
}
|
||||
// Locals of type unknown always considered initialized
|
||||
function f6() {
|
||||
var x;
|
||||
var y = x;
|
||||
}
|
||||
// Spread of unknown causes result to be unknown
|
||||
function f7(x, y, z) {
|
||||
var o1 = __assign({ a: 42 }, x); // { a: number }
|
||||
var o2 = __assign({ a: 42 }, x, y); // unknown
|
||||
var o3 = __assign({ a: 42 }, x, y, z); // any
|
||||
}
|
||||
// Functions with unknown return type don't need return expressions
|
||||
function f8() {
|
||||
}
|
||||
// Class properties of type unknown don't need definite assignment
|
||||
var C1 = /** @class */ (function () {
|
||||
function C1() {
|
||||
}
|
||||
return C1;
|
||||
}());
|
335
tests/baselines/reference/unknownType1.symbols
Normal file
335
tests/baselines/reference/unknownType1.symbols
Normal file
|
@ -0,0 +1,335 @@
|
|||
=== tests/cases/conformance/types/unknown/unknownType1.ts ===
|
||||
// In an intersection everything absorbs unknown
|
||||
|
||||
type T00 = unknown & null; // null
|
||||
>T00 : Symbol(T00, Decl(unknownType1.ts, 0, 0))
|
||||
|
||||
type T01 = unknown & undefined; // undefined
|
||||
>T01 : Symbol(T01, Decl(unknownType1.ts, 2, 26))
|
||||
|
||||
type T02 = unknown & null & undefined; // null & undefined (which becomes never in union)
|
||||
>T02 : Symbol(T02, Decl(unknownType1.ts, 3, 31))
|
||||
|
||||
type T03 = unknown & string; // string
|
||||
>T03 : Symbol(T03, Decl(unknownType1.ts, 4, 38))
|
||||
|
||||
type T04 = unknown & string[]; // string[]
|
||||
>T04 : Symbol(T04, Decl(unknownType1.ts, 5, 28))
|
||||
|
||||
type T05 = unknown & unknown; // unknown
|
||||
>T05 : Symbol(T05, Decl(unknownType1.ts, 6, 30))
|
||||
|
||||
type T06 = unknown & any; // any
|
||||
>T06 : Symbol(T06, Decl(unknownType1.ts, 7, 29))
|
||||
|
||||
// In a union an unknown absorbs everything
|
||||
|
||||
type T10 = unknown | null; // unknown
|
||||
>T10 : Symbol(T10, Decl(unknownType1.ts, 8, 25))
|
||||
|
||||
type T11 = unknown | undefined; // unknown
|
||||
>T11 : Symbol(T11, Decl(unknownType1.ts, 12, 26))
|
||||
|
||||
type T12 = unknown | null | undefined; // unknown
|
||||
>T12 : Symbol(T12, Decl(unknownType1.ts, 13, 31))
|
||||
|
||||
type T13 = unknown | string; // unknown
|
||||
>T13 : Symbol(T13, Decl(unknownType1.ts, 14, 38))
|
||||
|
||||
type T14 = unknown | string[]; // unknown
|
||||
>T14 : Symbol(T14, Decl(unknownType1.ts, 15, 28))
|
||||
|
||||
type T15 = unknown | unknown; // unknown
|
||||
>T15 : Symbol(T15, Decl(unknownType1.ts, 16, 30))
|
||||
|
||||
type T16 = unknown | any; // any
|
||||
>T16 : Symbol(T16, Decl(unknownType1.ts, 17, 29))
|
||||
|
||||
// Type variable and unknown in union and intersection
|
||||
|
||||
type T20<T> = T & {}; // T & {}
|
||||
>T20 : Symbol(T20, Decl(unknownType1.ts, 18, 25))
|
||||
>T : Symbol(T, Decl(unknownType1.ts, 22, 9))
|
||||
>T : Symbol(T, Decl(unknownType1.ts, 22, 9))
|
||||
|
||||
type T21<T> = T | {}; // T | {}
|
||||
>T21 : Symbol(T21, Decl(unknownType1.ts, 22, 21))
|
||||
>T : Symbol(T, Decl(unknownType1.ts, 23, 9))
|
||||
>T : Symbol(T, Decl(unknownType1.ts, 23, 9))
|
||||
|
||||
type T22<T> = T & unknown; // T
|
||||
>T22 : Symbol(T22, Decl(unknownType1.ts, 23, 21))
|
||||
>T : Symbol(T, Decl(unknownType1.ts, 24, 9))
|
||||
>T : Symbol(T, Decl(unknownType1.ts, 24, 9))
|
||||
|
||||
type T23<T> = T | unknown; // unknown
|
||||
>T23 : Symbol(T23, Decl(unknownType1.ts, 24, 26))
|
||||
>T : Symbol(T, Decl(unknownType1.ts, 25, 9))
|
||||
>T : Symbol(T, Decl(unknownType1.ts, 25, 9))
|
||||
|
||||
// unknown in conditional types
|
||||
|
||||
type T30<T> = unknown extends T ? true : false; // Deferred
|
||||
>T30 : Symbol(T30, Decl(unknownType1.ts, 25, 26))
|
||||
>T : Symbol(T, Decl(unknownType1.ts, 29, 9))
|
||||
>T : Symbol(T, Decl(unknownType1.ts, 29, 9))
|
||||
|
||||
type T31<T> = T extends unknown ? true : false; // true
|
||||
>T31 : Symbol(T31, Decl(unknownType1.ts, 29, 47))
|
||||
>T : Symbol(T, Decl(unknownType1.ts, 30, 9))
|
||||
>T : Symbol(T, Decl(unknownType1.ts, 30, 9))
|
||||
|
||||
type T32<T> = never extends T ? true : false; // true
|
||||
>T32 : Symbol(T32, Decl(unknownType1.ts, 30, 47))
|
||||
>T : Symbol(T, Decl(unknownType1.ts, 31, 9))
|
||||
>T : Symbol(T, Decl(unknownType1.ts, 31, 9))
|
||||
|
||||
type T33<T> = T extends never ? true : false; // Deferred
|
||||
>T33 : Symbol(T33, Decl(unknownType1.ts, 31, 45))
|
||||
>T : Symbol(T, Decl(unknownType1.ts, 32, 9))
|
||||
>T : Symbol(T, Decl(unknownType1.ts, 32, 9))
|
||||
|
||||
// keyof unknown
|
||||
|
||||
type T40 = keyof any; // string | number | symbol
|
||||
>T40 : Symbol(T40, Decl(unknownType1.ts, 32, 45))
|
||||
|
||||
type T41 = keyof unknown; // string | number | symbol
|
||||
>T41 : Symbol(T41, Decl(unknownType1.ts, 36, 21))
|
||||
|
||||
// typeof, instanceof, and user defined type predicates
|
||||
|
||||
declare function isFunction(x: unknown): x is Function;
|
||||
>isFunction : Symbol(isFunction, Decl(unknownType1.ts, 37, 25))
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 41, 28))
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 41, 28))
|
||||
>Function : Symbol(Function, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
|
||||
|
||||
function f1(x: unknown) {
|
||||
>f1 : Symbol(f1, Decl(unknownType1.ts, 41, 55))
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 43, 12))
|
||||
|
||||
if (typeof x === "string" || typeof x === "number") {
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 43, 12))
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 43, 12))
|
||||
|
||||
x; // string | number
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 43, 12))
|
||||
}
|
||||
if (typeof x === "object") {
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 43, 12))
|
||||
|
||||
x; // object
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 43, 12))
|
||||
}
|
||||
if (typeof x === "function") {
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 43, 12))
|
||||
|
||||
x; // object
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 43, 12))
|
||||
}
|
||||
if (x instanceof Error) {
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 43, 12))
|
||||
>Error : Symbol(Error, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
|
||||
|
||||
x; // Error
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 43, 12))
|
||||
}
|
||||
if (isFunction(x)) {
|
||||
>isFunction : Symbol(isFunction, Decl(unknownType1.ts, 37, 25))
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 43, 12))
|
||||
|
||||
x; // Function
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 43, 12))
|
||||
}
|
||||
}
|
||||
|
||||
// Homomorphic mapped type over unknown
|
||||
|
||||
type T50<T> = { [P in keyof T]: number };
|
||||
>T50 : Symbol(T50, Decl(unknownType1.ts, 59, 1))
|
||||
>T : Symbol(T, Decl(unknownType1.ts, 63, 9))
|
||||
>P : Symbol(P, Decl(unknownType1.ts, 63, 17))
|
||||
>T : Symbol(T, Decl(unknownType1.ts, 63, 9))
|
||||
|
||||
type T51 = T50<unknown>; // { [x: string]: number }
|
||||
>T51 : Symbol(T51, Decl(unknownType1.ts, 63, 41))
|
||||
>T50 : Symbol(T50, Decl(unknownType1.ts, 59, 1))
|
||||
|
||||
type T52 = T50<any>; // { [x: string]: number }
|
||||
>T52 : Symbol(T52, Decl(unknownType1.ts, 64, 24))
|
||||
>T50 : Symbol(T50, Decl(unknownType1.ts, 59, 1))
|
||||
|
||||
// Anything is assignable to unknown
|
||||
|
||||
function f2<T>(pAny: any, pNever: never, pT: T) {
|
||||
>f2 : Symbol(f2, Decl(unknownType1.ts, 65, 20))
|
||||
>T : Symbol(T, Decl(unknownType1.ts, 69, 12))
|
||||
>pAny : Symbol(pAny, Decl(unknownType1.ts, 69, 15))
|
||||
>pNever : Symbol(pNever, Decl(unknownType1.ts, 69, 25))
|
||||
>pT : Symbol(pT, Decl(unknownType1.ts, 69, 40))
|
||||
>T : Symbol(T, Decl(unknownType1.ts, 69, 12))
|
||||
|
||||
let x: unknown;
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 70, 7))
|
||||
|
||||
x = 123;
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 70, 7))
|
||||
|
||||
x = "hello";
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 70, 7))
|
||||
|
||||
x = [1, 2, 3];
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 70, 7))
|
||||
|
||||
x = new Error();
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 70, 7))
|
||||
>Error : Symbol(Error, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
|
||||
|
||||
x = x;
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 70, 7))
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 70, 7))
|
||||
|
||||
x = pAny;
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 70, 7))
|
||||
>pAny : Symbol(pAny, Decl(unknownType1.ts, 69, 15))
|
||||
|
||||
x = pNever;
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 70, 7))
|
||||
>pNever : Symbol(pNever, Decl(unknownType1.ts, 69, 25))
|
||||
|
||||
x = pT;
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 70, 7))
|
||||
>pT : Symbol(pT, Decl(unknownType1.ts, 69, 40))
|
||||
}
|
||||
|
||||
// unknown assignable only to itself and any
|
||||
|
||||
function f3(x: unknown) {
|
||||
>f3 : Symbol(f3, Decl(unknownType1.ts, 79, 1))
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 83, 12))
|
||||
|
||||
let v1: any = x;
|
||||
>v1 : Symbol(v1, Decl(unknownType1.ts, 84, 7))
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 83, 12))
|
||||
|
||||
let v2: unknown = x;
|
||||
>v2 : Symbol(v2, Decl(unknownType1.ts, 85, 7))
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 83, 12))
|
||||
|
||||
let v3: object = x; // Error
|
||||
>v3 : Symbol(v3, Decl(unknownType1.ts, 86, 7))
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 83, 12))
|
||||
|
||||
let v4: string = x; // Error
|
||||
>v4 : Symbol(v4, Decl(unknownType1.ts, 87, 7))
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 83, 12))
|
||||
|
||||
let v5: string[] = x; // Error
|
||||
>v5 : Symbol(v5, Decl(unknownType1.ts, 88, 7))
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 83, 12))
|
||||
|
||||
let v6: {} = x; // Error
|
||||
>v6 : Symbol(v6, Decl(unknownType1.ts, 89, 7))
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 83, 12))
|
||||
|
||||
let v7: {} | null | undefined = x; // Error
|
||||
>v7 : Symbol(v7, Decl(unknownType1.ts, 90, 7))
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 83, 12))
|
||||
}
|
||||
|
||||
// Type parameter 'T extends unknown' not related to object
|
||||
|
||||
function f4<T extends unknown>(x: T) {
|
||||
>f4 : Symbol(f4, Decl(unknownType1.ts, 91, 1))
|
||||
>T : Symbol(T, Decl(unknownType1.ts, 95, 12))
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 95, 31))
|
||||
>T : Symbol(T, Decl(unknownType1.ts, 95, 12))
|
||||
|
||||
let y: object = x; // Error
|
||||
>y : Symbol(y, Decl(unknownType1.ts, 96, 7))
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 95, 31))
|
||||
}
|
||||
|
||||
// Anything but primitive assignable to { [x: string]: unknown }
|
||||
|
||||
function f5(x: { [x: string]: unknown }) {
|
||||
>f5 : Symbol(f5, Decl(unknownType1.ts, 97, 1))
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 101, 12))
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 101, 18))
|
||||
|
||||
x = {};
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 101, 12))
|
||||
|
||||
x = { a: 5 };
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 101, 12))
|
||||
>a : Symbol(a, Decl(unknownType1.ts, 103, 9))
|
||||
|
||||
x = [1, 2, 3];
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 101, 12))
|
||||
|
||||
x = 123; // Error
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 101, 12))
|
||||
}
|
||||
|
||||
// Locals of type unknown always considered initialized
|
||||
|
||||
function f6() {
|
||||
>f6 : Symbol(f6, Decl(unknownType1.ts, 106, 1))
|
||||
|
||||
let x: unknown;
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 111, 7))
|
||||
|
||||
let y = x;
|
||||
>y : Symbol(y, Decl(unknownType1.ts, 112, 7))
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 111, 7))
|
||||
}
|
||||
|
||||
// Spread of unknown causes result to be unknown
|
||||
|
||||
function f7(x: {}, y: unknown, z: any) {
|
||||
>f7 : Symbol(f7, Decl(unknownType1.ts, 113, 1))
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 117, 12))
|
||||
>y : Symbol(y, Decl(unknownType1.ts, 117, 18))
|
||||
>z : Symbol(z, Decl(unknownType1.ts, 117, 30))
|
||||
|
||||
let o1 = { a: 42, ...x }; // { a: number }
|
||||
>o1 : Symbol(o1, Decl(unknownType1.ts, 118, 7))
|
||||
>a : Symbol(a, Decl(unknownType1.ts, 118, 14))
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 117, 12))
|
||||
|
||||
let o2 = { a: 42, ...x, ...y }; // unknown
|
||||
>o2 : Symbol(o2, Decl(unknownType1.ts, 119, 7))
|
||||
>a : Symbol(a, Decl(unknownType1.ts, 119, 14))
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 117, 12))
|
||||
>y : Symbol(y, Decl(unknownType1.ts, 117, 18))
|
||||
|
||||
let o3 = { a: 42, ...x, ...y, ...z }; // any
|
||||
>o3 : Symbol(o3, Decl(unknownType1.ts, 120, 7))
|
||||
>a : Symbol(a, Decl(unknownType1.ts, 120, 14))
|
||||
>x : Symbol(x, Decl(unknownType1.ts, 117, 12))
|
||||
>y : Symbol(y, Decl(unknownType1.ts, 117, 18))
|
||||
>z : Symbol(z, Decl(unknownType1.ts, 117, 30))
|
||||
}
|
||||
|
||||
// Functions with unknown return type don't need return expressions
|
||||
|
||||
function f8(): unknown {
|
||||
>f8 : Symbol(f8, Decl(unknownType1.ts, 121, 1))
|
||||
}
|
||||
|
||||
// Class properties of type unknown don't need definite assignment
|
||||
|
||||
class C1 {
|
||||
>C1 : Symbol(C1, Decl(unknownType1.ts, 126, 1))
|
||||
|
||||
a: string; // Error
|
||||
>a : Symbol(C1.a, Decl(unknownType1.ts, 130, 10))
|
||||
|
||||
b: unknown;
|
||||
>b : Symbol(C1.b, Decl(unknownType1.ts, 131, 14))
|
||||
|
||||
c: any;
|
||||
>c : Symbol(C1.c, Decl(unknownType1.ts, 132, 15))
|
||||
}
|
||||
|
396
tests/baselines/reference/unknownType1.types
Normal file
396
tests/baselines/reference/unknownType1.types
Normal file
|
@ -0,0 +1,396 @@
|
|||
=== tests/cases/conformance/types/unknown/unknownType1.ts ===
|
||||
// In an intersection everything absorbs unknown
|
||||
|
||||
type T00 = unknown & null; // null
|
||||
>T00 : null
|
||||
>null : null
|
||||
|
||||
type T01 = unknown & undefined; // undefined
|
||||
>T01 : undefined
|
||||
|
||||
type T02 = unknown & null & undefined; // null & undefined (which becomes never in union)
|
||||
>T02 : T02
|
||||
>null : null
|
||||
|
||||
type T03 = unknown & string; // string
|
||||
>T03 : string
|
||||
|
||||
type T04 = unknown & string[]; // string[]
|
||||
>T04 : string[]
|
||||
|
||||
type T05 = unknown & unknown; // unknown
|
||||
>T05 : unknown
|
||||
|
||||
type T06 = unknown & any; // any
|
||||
>T06 : any
|
||||
|
||||
// In a union an unknown absorbs everything
|
||||
|
||||
type T10 = unknown | null; // unknown
|
||||
>T10 : unknown
|
||||
>null : null
|
||||
|
||||
type T11 = unknown | undefined; // unknown
|
||||
>T11 : unknown
|
||||
|
||||
type T12 = unknown | null | undefined; // unknown
|
||||
>T12 : unknown
|
||||
>null : null
|
||||
|
||||
type T13 = unknown | string; // unknown
|
||||
>T13 : unknown
|
||||
|
||||
type T14 = unknown | string[]; // unknown
|
||||
>T14 : unknown
|
||||
|
||||
type T15 = unknown | unknown; // unknown
|
||||
>T15 : unknown
|
||||
|
||||
type T16 = unknown | any; // any
|
||||
>T16 : any
|
||||
|
||||
// Type variable and unknown in union and intersection
|
||||
|
||||
type T20<T> = T & {}; // T & {}
|
||||
>T20 : T20<T>
|
||||
>T : T
|
||||
>T : T
|
||||
|
||||
type T21<T> = T | {}; // T | {}
|
||||
>T21 : T21<T>
|
||||
>T : T
|
||||
>T : T
|
||||
|
||||
type T22<T> = T & unknown; // T
|
||||
>T22 : T
|
||||
>T : T
|
||||
>T : T
|
||||
|
||||
type T23<T> = T | unknown; // unknown
|
||||
>T23 : unknown
|
||||
>T : T
|
||||
>T : T
|
||||
|
||||
// unknown in conditional types
|
||||
|
||||
type T30<T> = unknown extends T ? true : false; // Deferred
|
||||
>T30 : T30<T>
|
||||
>T : T
|
||||
>T : T
|
||||
>true : true
|
||||
>false : false
|
||||
|
||||
type T31<T> = T extends unknown ? true : false; // true
|
||||
>T31 : true
|
||||
>T : T
|
||||
>T : T
|
||||
>true : true
|
||||
>false : false
|
||||
|
||||
type T32<T> = never extends T ? true : false; // true
|
||||
>T32 : true
|
||||
>T : T
|
||||
>T : T
|
||||
>true : true
|
||||
>false : false
|
||||
|
||||
type T33<T> = T extends never ? true : false; // Deferred
|
||||
>T33 : T33<T>
|
||||
>T : T
|
||||
>T : T
|
||||
>true : true
|
||||
>false : false
|
||||
|
||||
// keyof unknown
|
||||
|
||||
type T40 = keyof any; // string | number | symbol
|
||||
>T40 : string | number | symbol
|
||||
|
||||
type T41 = keyof unknown; // string | number | symbol
|
||||
>T41 : string | number | symbol
|
||||
|
||||
// typeof, instanceof, and user defined type predicates
|
||||
|
||||
declare function isFunction(x: unknown): x is Function;
|
||||
>isFunction : (x: unknown) => x is Function
|
||||
>x : unknown
|
||||
>x : any
|
||||
>Function : Function
|
||||
|
||||
function f1(x: unknown) {
|
||||
>f1 : (x: unknown) => void
|
||||
>x : unknown
|
||||
|
||||
if (typeof x === "string" || typeof x === "number") {
|
||||
>typeof x === "string" || typeof x === "number" : boolean
|
||||
>typeof x === "string" : boolean
|
||||
>typeof x : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
|
||||
>x : unknown
|
||||
>"string" : "string"
|
||||
>typeof x === "number" : boolean
|
||||
>typeof x : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
|
||||
>x : unknown
|
||||
>"number" : "number"
|
||||
|
||||
x; // string | number
|
||||
>x : string | number
|
||||
}
|
||||
if (typeof x === "object") {
|
||||
>typeof x === "object" : boolean
|
||||
>typeof x : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
|
||||
>x : unknown
|
||||
>"object" : "object"
|
||||
|
||||
x; // object
|
||||
>x : object
|
||||
}
|
||||
if (typeof x === "function") {
|
||||
>typeof x === "function" : boolean
|
||||
>typeof x : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
|
||||
>x : unknown
|
||||
>"function" : "function"
|
||||
|
||||
x; // object
|
||||
>x : object
|
||||
}
|
||||
if (x instanceof Error) {
|
||||
>x instanceof Error : boolean
|
||||
>x : unknown
|
||||
>Error : ErrorConstructor
|
||||
|
||||
x; // Error
|
||||
>x : Error
|
||||
}
|
||||
if (isFunction(x)) {
|
||||
>isFunction(x) : boolean
|
||||
>isFunction : (x: unknown) => x is Function
|
||||
>x : unknown
|
||||
|
||||
x; // Function
|
||||
>x : Function
|
||||
}
|
||||
}
|
||||
|
||||
// Homomorphic mapped type over unknown
|
||||
|
||||
type T50<T> = { [P in keyof T]: number };
|
||||
>T50 : T50<T>
|
||||
>T : T
|
||||
>P : P
|
||||
>T : T
|
||||
|
||||
type T51 = T50<unknown>; // { [x: string]: number }
|
||||
>T51 : T50<unknown>
|
||||
>T50 : T50<T>
|
||||
|
||||
type T52 = T50<any>; // { [x: string]: number }
|
||||
>T52 : T50<any>
|
||||
>T50 : T50<T>
|
||||
|
||||
// Anything is assignable to unknown
|
||||
|
||||
function f2<T>(pAny: any, pNever: never, pT: T) {
|
||||
>f2 : <T>(pAny: any, pNever: never, pT: T) => void
|
||||
>T : T
|
||||
>pAny : any
|
||||
>pNever : never
|
||||
>pT : T
|
||||
>T : T
|
||||
|
||||
let x: unknown;
|
||||
>x : unknown
|
||||
|
||||
x = 123;
|
||||
>x = 123 : 123
|
||||
>x : unknown
|
||||
>123 : 123
|
||||
|
||||
x = "hello";
|
||||
>x = "hello" : "hello"
|
||||
>x : unknown
|
||||
>"hello" : "hello"
|
||||
|
||||
x = [1, 2, 3];
|
||||
>x = [1, 2, 3] : number[]
|
||||
>x : unknown
|
||||
>[1, 2, 3] : number[]
|
||||
>1 : 1
|
||||
>2 : 2
|
||||
>3 : 3
|
||||
|
||||
x = new Error();
|
||||
>x = new Error() : Error
|
||||
>x : unknown
|
||||
>new Error() : Error
|
||||
>Error : ErrorConstructor
|
||||
|
||||
x = x;
|
||||
>x = x : unknown
|
||||
>x : unknown
|
||||
>x : unknown
|
||||
|
||||
x = pAny;
|
||||
>x = pAny : any
|
||||
>x : unknown
|
||||
>pAny : any
|
||||
|
||||
x = pNever;
|
||||
>x = pNever : never
|
||||
>x : unknown
|
||||
>pNever : never
|
||||
|
||||
x = pT;
|
||||
>x = pT : T
|
||||
>x : unknown
|
||||
>pT : T
|
||||
}
|
||||
|
||||
// unknown assignable only to itself and any
|
||||
|
||||
function f3(x: unknown) {
|
||||
>f3 : (x: unknown) => void
|
||||
>x : unknown
|
||||
|
||||
let v1: any = x;
|
||||
>v1 : any
|
||||
>x : unknown
|
||||
|
||||
let v2: unknown = x;
|
||||
>v2 : unknown
|
||||
>x : unknown
|
||||
|
||||
let v3: object = x; // Error
|
||||
>v3 : object
|
||||
>x : unknown
|
||||
|
||||
let v4: string = x; // Error
|
||||
>v4 : string
|
||||
>x : unknown
|
||||
|
||||
let v5: string[] = x; // Error
|
||||
>v5 : string[]
|
||||
>x : unknown
|
||||
|
||||
let v6: {} = x; // Error
|
||||
>v6 : {}
|
||||
>x : unknown
|
||||
|
||||
let v7: {} | null | undefined = x; // Error
|
||||
>v7 : {} | null | undefined
|
||||
>null : null
|
||||
>x : unknown
|
||||
}
|
||||
|
||||
// Type parameter 'T extends unknown' not related to object
|
||||
|
||||
function f4<T extends unknown>(x: T) {
|
||||
>f4 : <T extends unknown>(x: T) => void
|
||||
>T : T
|
||||
>x : T
|
||||
>T : T
|
||||
|
||||
let y: object = x; // Error
|
||||
>y : object
|
||||
>x : T
|
||||
}
|
||||
|
||||
// Anything but primitive assignable to { [x: string]: unknown }
|
||||
|
||||
function f5(x: { [x: string]: unknown }) {
|
||||
>f5 : (x: { [x: string]: unknown; }) => void
|
||||
>x : { [x: string]: unknown; }
|
||||
>x : string
|
||||
|
||||
x = {};
|
||||
>x = {} : {}
|
||||
>x : { [x: string]: unknown; }
|
||||
>{} : {}
|
||||
|
||||
x = { a: 5 };
|
||||
>x = { a: 5 } : { a: number; }
|
||||
>x : { [x: string]: unknown; }
|
||||
>{ a: 5 } : { a: number; }
|
||||
>a : number
|
||||
>5 : 5
|
||||
|
||||
x = [1, 2, 3];
|
||||
>x = [1, 2, 3] : number[]
|
||||
>x : { [x: string]: unknown; }
|
||||
>[1, 2, 3] : number[]
|
||||
>1 : 1
|
||||
>2 : 2
|
||||
>3 : 3
|
||||
|
||||
x = 123; // Error
|
||||
>x = 123 : 123
|
||||
>x : { [x: string]: unknown; }
|
||||
>123 : 123
|
||||
}
|
||||
|
||||
// Locals of type unknown always considered initialized
|
||||
|
||||
function f6() {
|
||||
>f6 : () => void
|
||||
|
||||
let x: unknown;
|
||||
>x : unknown
|
||||
|
||||
let y = x;
|
||||
>y : unknown
|
||||
>x : unknown
|
||||
}
|
||||
|
||||
// Spread of unknown causes result to be unknown
|
||||
|
||||
function f7(x: {}, y: unknown, z: any) {
|
||||
>f7 : (x: {}, y: unknown, z: any) => void
|
||||
>x : {}
|
||||
>y : unknown
|
||||
>z : any
|
||||
|
||||
let o1 = { a: 42, ...x }; // { a: number }
|
||||
>o1 : { a: number; }
|
||||
>{ a: 42, ...x } : { a: number; }
|
||||
>a : number
|
||||
>42 : 42
|
||||
>x : {}
|
||||
|
||||
let o2 = { a: 42, ...x, ...y }; // unknown
|
||||
>o2 : unknown
|
||||
>{ a: 42, ...x, ...y } : unknown
|
||||
>a : number
|
||||
>42 : 42
|
||||
>x : {}
|
||||
>y : unknown
|
||||
|
||||
let o3 = { a: 42, ...x, ...y, ...z }; // any
|
||||
>o3 : any
|
||||
>{ a: 42, ...x, ...y, ...z } : any
|
||||
>a : number
|
||||
>42 : 42
|
||||
>x : {}
|
||||
>y : unknown
|
||||
>z : any
|
||||
}
|
||||
|
||||
// Functions with unknown return type don't need return expressions
|
||||
|
||||
function f8(): unknown {
|
||||
>f8 : () => unknown
|
||||
}
|
||||
|
||||
// Class properties of type unknown don't need definite assignment
|
||||
|
||||
class C1 {
|
||||
>C1 : C1
|
||||
|
||||
a: string; // Error
|
||||
>a : string
|
||||
|
||||
b: unknown;
|
||||
>b : unknown
|
||||
|
||||
c: any;
|
||||
>c : any
|
||||
}
|
||||
|
Loading…
Reference in a new issue