Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ namespace ts {
"symbol": {
type: esSymbolType,
flags: TypeFlags.ESSymbol
},
"undefined": {
type: undefinedType,
flags: TypeFlags.ContainsUndefinedOrNull
}
};

Expand Down Expand Up @@ -6475,6 +6479,10 @@ namespace ts {
assumeTrue = !assumeTrue;
}
const typeInfo = primitiveTypeInfo[right.text];
// Don't narrow `undefined`
if (typeInfo && typeInfo.type === undefinedType) {
return type;
}
// If the type to be narrowed is any and we're checking a primitive with assumeTrue=true, return the primitive
if (!!(type.flags & TypeFlags.Any) && typeInfo && assumeTrue) {
return typeInfo.type;
Expand Down
357 changes: 357 additions & 0 deletions tests/baselines/reference/typeGuardTypeOfUndefined.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,357 @@
//// [typeGuardTypeOfUndefined.ts]
// undefined type guard adds no new type information
function test1(a: any) {
if (typeof a !== "undefined") {
if (typeof a === "boolean") {
a;
}
else {
a;
}
}
else {
a;
}
}

function test2(a: any) {
if (typeof a === "undefined") {
if (typeof a === "boolean") {
a;
}
else {
a;
}
}
else {
a;
}
}

function test3(a: any) {
if (typeof a === "undefined" || typeof a === "boolean") {
a;
}
else {
a;
}
}

function test4(a: any) {
if (typeof a !== "undefined" && typeof a === "boolean") {
a;
}
else {
a;
}
}

function test5(a: boolean | void) {
if (typeof a !== "undefined") {
if (typeof a === "boolean") {
a;
}
else {
a;
}
}
else {
a;
}
}

function test6(a: boolean | void) {
if (typeof a === "undefined") {
if (typeof a === "boolean") {
a;
}
else {
a;
}
}
else {
a;
}
}

function test7(a: boolean | void) {
if (typeof a === "undefined" || typeof a === "boolean") {
a;
}
else {
a;
}
}

function test8(a: boolean | void) {
if (typeof a !== "undefined" && typeof a === "boolean") {
a;
}
else {
a;
}
}

function test9(a: boolean | number) {
if (typeof a !== "undefined") {
if (typeof a === "boolean") {
a;
}
else {
a;
}
}
else {
a;
}
}

function test10(a: boolean | number) {
if (typeof a === "undefined") {
if (typeof a === "boolean") {
a;
}
else {
a;
}
}
else {
a;
}
}

function test11(a: boolean | number) {
if (typeof a === "undefined" || typeof a === "boolean") {
a;
}
else {
a;
}
}

function test12(a: boolean | number) {
if (typeof a !== "undefined" && typeof a === "boolean") {
a;
}
else {
a;
}
}

function test13(a: boolean | number | void) {
if (typeof a !== "undefined") {
if (typeof a === "boolean") {
a;
}
else {
a;
}
}
else {
a;
}
}

function test14(a: boolean | number | void) {
if (typeof a === "undefined") {
if (typeof a === "boolean") {
a;
}
else {
a;
}
}
else {
a;
}
}

function test15(a: boolean | number | void) {
if (typeof a === "undefined" || typeof a === "boolean") {
a;
}
else {
a;
}
}

function test16(a: boolean | number | void) {
if (typeof a !== "undefined" && typeof a === "boolean") {
a;
}
else {
a;
}
}


//// [typeGuardTypeOfUndefined.js]
// undefined type guard adds no new type information
function test1(a) {
if (typeof a !== "undefined") {
if (typeof a === "boolean") {
a;
}
else {
a;
}
}
else {
a;
}
}
function test2(a) {
if (typeof a === "undefined") {
if (typeof a === "boolean") {
a;
}
else {
a;
}
}
else {
a;
}
}
function test3(a) {
if (typeof a === "undefined" || typeof a === "boolean") {
a;
}
else {
a;
}
}
function test4(a) {
if (typeof a !== "undefined" && typeof a === "boolean") {
a;
}
else {
a;
}
}
function test5(a) {
if (typeof a !== "undefined") {
if (typeof a === "boolean") {
a;
}
else {
a;
}
}
else {
a;
}
}
function test6(a) {
if (typeof a === "undefined") {
if (typeof a === "boolean") {
a;
}
else {
a;
}
}
else {
a;
}
}
function test7(a) {
if (typeof a === "undefined" || typeof a === "boolean") {
a;
}
else {
a;
}
}
function test8(a) {
if (typeof a !== "undefined" && typeof a === "boolean") {
a;
}
else {
a;
}
}
function test9(a) {
if (typeof a !== "undefined") {
if (typeof a === "boolean") {
a;
}
else {
a;
}
}
else {
a;
}
}
function test10(a) {
if (typeof a === "undefined") {
if (typeof a === "boolean") {
a;
}
else {
a;
}
}
else {
a;
}
}
function test11(a) {
if (typeof a === "undefined" || typeof a === "boolean") {
a;
}
else {
a;
}
}
function test12(a) {
if (typeof a !== "undefined" && typeof a === "boolean") {
a;
}
else {
a;
}
}
function test13(a) {
if (typeof a !== "undefined") {
if (typeof a === "boolean") {
a;
}
else {
a;
}
}
else {
a;
}
}
function test14(a) {
if (typeof a === "undefined") {
if (typeof a === "boolean") {
a;
}
else {
a;
}
}
else {
a;
}
}
function test15(a) {
if (typeof a === "undefined" || typeof a === "boolean") {
a;
}
else {
a;
}
}
function test16(a) {
if (typeof a !== "undefined" && typeof a === "boolean") {
a;
}
else {
a;
}
}
Loading