Skip to content

Commit

Permalink
add union type test to baselines
Browse files Browse the repository at this point in the history
  • Loading branch information
danvk committed Feb 21, 2024
1 parent 101df93 commit d0e385e
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
9 changes: 9 additions & 0 deletions tests/baselines/reference/inferTypePredicates.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,13 @@ inferTypePredicates.ts(205,7): error TS2741: Property 'z' is missing in type 'C1
} else {
let t: never = str; // should OK
}

// infer a union type
function isNumOrStr(x: unknown) {
return (typeof x === "number" || typeof x === "string");
}
declare let unk: unknown;
if (isNumOrStr(unk)) {
let t: number | string = unk; // should ok
}

16 changes: 16 additions & 0 deletions tests/baselines/reference/inferTypePredicates.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,15 @@ if (isStringFromUnknown(str)) {
} else {
let t: never = str; // should OK
}

// infer a union type
function isNumOrStr(x: unknown) {
return (typeof x === "number" || typeof x === "string");
}
declare let unk: unknown;
if (isNumOrStr(unk)) {
let t: number | string = unk; // should ok
}


//// [inferTypePredicates.js]
Expand Down Expand Up @@ -443,3 +452,10 @@ if (isStringFromUnknown(str)) {
else {
var t = str; // should OK
}
// infer a union type
function isNumOrStr(x) {
return (typeof x === "number" || typeof x === "string");
}
if (isNumOrStr(unk)) {
var t = unk; // should ok
}
21 changes: 21 additions & 0 deletions tests/baselines/reference/inferTypePredicates.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -659,3 +659,24 @@ if (isStringFromUnknown(str)) {
>str : Symbol(str, Decl(inferTypePredicates.ts, 216, 11))
}

// infer a union type
function isNumOrStr(x: unknown) {
>isNumOrStr : Symbol(isNumOrStr, Decl(inferTypePredicates.ts, 230, 1))
>x : Symbol(x, Decl(inferTypePredicates.ts, 233, 20))

return (typeof x === "number" || typeof x === "string");
>x : Symbol(x, Decl(inferTypePredicates.ts, 233, 20))
>x : Symbol(x, Decl(inferTypePredicates.ts, 233, 20))
}
declare let unk: unknown;
>unk : Symbol(unk, Decl(inferTypePredicates.ts, 236, 11))

if (isNumOrStr(unk)) {
>isNumOrStr : Symbol(isNumOrStr, Decl(inferTypePredicates.ts, 230, 1))
>unk : Symbol(unk, Decl(inferTypePredicates.ts, 236, 11))

let t: number | string = unk; // should ok
>t : Symbol(t, Decl(inferTypePredicates.ts, 238, 5))
>unk : Symbol(unk, Decl(inferTypePredicates.ts, 236, 11))
}

30 changes: 30 additions & 0 deletions tests/baselines/reference/inferTypePredicates.types
Original file line number Diff line number Diff line change
Expand Up @@ -854,3 +854,33 @@ if (isStringFromUnknown(str)) {
>str : never
}

// infer a union type
function isNumOrStr(x: unknown) {
>isNumOrStr : (x: unknown) => x is string | number
>x : unknown

return (typeof x === "number" || typeof x === "string");
>(typeof x === "number" || typeof x === "string") : boolean
>typeof x === "number" || typeof x === "string" : boolean
>typeof x === "number" : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : unknown
>"number" : "number"
>typeof x === "string" : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : unknown
>"string" : "string"
}
declare let unk: unknown;
>unk : unknown

if (isNumOrStr(unk)) {
>isNumOrStr(unk) : boolean
>isNumOrStr : (x: unknown) => x is string | number
>unk : unknown

let t: number | string = unk; // should ok
>t : string | number
>unk : string | number
}

0 comments on commit d0e385e

Please sign in to comment.