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
|
//// [nonNullableTypes1.ts]
function f1<T>(x: T) {
let y = x || "hello"; // NonNullable<T> | string
}
function error(): never {
throw new Error();
}
function f2<T>(x: T) { // NonNullable<T>
return x || error();
}
function f3(x: unknown) {
let y = x!; // {}
}
function f4<T extends { x: string } | undefined>(obj: T) {
if (obj?.x === "hello") {
obj; // NonNullable<T>
}
if (obj?.x) {
obj; // NonNullable<T>
}
if (typeof obj?.x === "string") {
obj; // NonNullable<T>
}
}
class A {
x = "hello";
foo() {
let zz = this?.x; // string
}
}
//// [nonNullableTypes1.js]
"use strict";
function f1(x) {
var y = x || "hello"; // NonNullable<T> | string
}
function error() {
throw new Error();
}
function f2(x) {
return x || error();
}
function f3(x) {
var y = x; // {}
}
function f4(obj) {
if ((obj === null || obj === void 0 ? void 0 : obj.x) === "hello") {
obj; // NonNullable<T>
}
if (obj === null || obj === void 0 ? void 0 : obj.x) {
obj; // NonNullable<T>
}
if (typeof (obj === null || obj === void 0 ? void 0 : obj.x) === "string") {
obj; // NonNullable<T>
}
}
var A = /** @class */ (function () {
function A() {
this.x = "hello";
}
A.prototype.foo = function () {
var zz = this === null || this === void 0 ? void 0 : this.x; // string
};
return A;
}());
//// [nonNullableTypes1.d.ts]
declare function f1<T>(x: T): void;
declare function error(): never;
declare function f2<T>(x: T): NonNullable<T>;
declare function f3(x: unknown): void;
declare function f4<T extends {
x: string;
} | undefined>(obj: T): void;
declare class A {
x: string;
foo(): void;
}
|