mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-07-02 14:48:32 -05:00
Add test case for a predicate that throws
This commit is contained in:
@@ -284,4 +284,19 @@ inferTypePredicates.ts(205,7): error TS2741: Property 'z' is missing in type 'C1
|
||||
if (isNumOrStr(unk)) {
|
||||
let t: number | string = unk; // should ok
|
||||
}
|
||||
|
||||
// A function can be a type predicate even if it throws.
|
||||
function assertAndPredicate(x: string | number | Date) {
|
||||
if (x instanceof Date) {
|
||||
throw new Error();
|
||||
}
|
||||
return typeof x === 'string';
|
||||
}
|
||||
|
||||
declare let snd: string | number | Date;
|
||||
if (assertAndPredicate(snd)) {
|
||||
let t: string = snd; // should ok
|
||||
} else {
|
||||
snd; // type is number | Date
|
||||
}
|
||||
|
||||
@@ -241,6 +241,21 @@ declare let unk: unknown;
|
||||
if (isNumOrStr(unk)) {
|
||||
let t: number | string = unk; // should ok
|
||||
}
|
||||
|
||||
// A function can be a type predicate even if it throws.
|
||||
function assertAndPredicate(x: string | number | Date) {
|
||||
if (x instanceof Date) {
|
||||
throw new Error();
|
||||
}
|
||||
return typeof x === 'string';
|
||||
}
|
||||
|
||||
declare let snd: string | number | Date;
|
||||
if (assertAndPredicate(snd)) {
|
||||
let t: string = snd; // should ok
|
||||
} else {
|
||||
snd; // type is number | Date
|
||||
}
|
||||
|
||||
|
||||
//// [inferTypePredicates.js]
|
||||
@@ -459,3 +474,16 @@ function isNumOrStr(x) {
|
||||
if (isNumOrStr(unk)) {
|
||||
var t = unk; // should ok
|
||||
}
|
||||
// A function can be a type predicate even if it throws.
|
||||
function assertAndPredicate(x) {
|
||||
if (x instanceof Date) {
|
||||
throw new Error();
|
||||
}
|
||||
return typeof x === 'string';
|
||||
}
|
||||
if (assertAndPredicate(snd)) {
|
||||
var t = snd; // should ok
|
||||
}
|
||||
else {
|
||||
snd; // type is number | Date
|
||||
}
|
||||
|
||||
@@ -680,3 +680,37 @@ if (isNumOrStr(unk)) {
|
||||
>unk : Symbol(unk, Decl(inferTypePredicates.ts, 236, 11))
|
||||
}
|
||||
|
||||
// A function can be a type predicate even if it throws.
|
||||
function assertAndPredicate(x: string | number | Date) {
|
||||
>assertAndPredicate : Symbol(assertAndPredicate, Decl(inferTypePredicates.ts, 239, 1))
|
||||
>x : Symbol(x, Decl(inferTypePredicates.ts, 242, 28))
|
||||
>Date : Symbol(Date, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.scripthost.d.ts, --, --))
|
||||
|
||||
if (x instanceof Date) {
|
||||
>x : Symbol(x, Decl(inferTypePredicates.ts, 242, 28))
|
||||
>Date : Symbol(Date, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.scripthost.d.ts, --, --))
|
||||
|
||||
throw new Error();
|
||||
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
|
||||
}
|
||||
return typeof x === 'string';
|
||||
>x : Symbol(x, Decl(inferTypePredicates.ts, 242, 28))
|
||||
}
|
||||
|
||||
declare let snd: string | number | Date;
|
||||
>snd : Symbol(snd, Decl(inferTypePredicates.ts, 249, 11))
|
||||
>Date : Symbol(Date, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.scripthost.d.ts, --, --))
|
||||
|
||||
if (assertAndPredicate(snd)) {
|
||||
>assertAndPredicate : Symbol(assertAndPredicate, Decl(inferTypePredicates.ts, 239, 1))
|
||||
>snd : Symbol(snd, Decl(inferTypePredicates.ts, 249, 11))
|
||||
|
||||
let t: string = snd; // should ok
|
||||
>t : Symbol(t, Decl(inferTypePredicates.ts, 251, 5))
|
||||
>snd : Symbol(snd, Decl(inferTypePredicates.ts, 249, 11))
|
||||
|
||||
} else {
|
||||
snd; // type is number | Date
|
||||
>snd : Symbol(snd, Decl(inferTypePredicates.ts, 249, 11))
|
||||
}
|
||||
|
||||
|
||||
@@ -884,3 +884,41 @@ if (isNumOrStr(unk)) {
|
||||
>unk : string | number
|
||||
}
|
||||
|
||||
// A function can be a type predicate even if it throws.
|
||||
function assertAndPredicate(x: string | number | Date) {
|
||||
>assertAndPredicate : (x: string | number | Date) => x is string
|
||||
>x : string | number | Date
|
||||
|
||||
if (x instanceof Date) {
|
||||
>x instanceof Date : boolean
|
||||
>x : string | number | Date
|
||||
>Date : DateConstructor
|
||||
|
||||
throw new Error();
|
||||
>new Error() : Error
|
||||
>Error : ErrorConstructor
|
||||
}
|
||||
return typeof x === 'string';
|
||||
>typeof x === 'string' : boolean
|
||||
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
|
||||
>x : string | number
|
||||
>'string' : "string"
|
||||
}
|
||||
|
||||
declare let snd: string | number | Date;
|
||||
>snd : string | number | Date
|
||||
|
||||
if (assertAndPredicate(snd)) {
|
||||
>assertAndPredicate(snd) : boolean
|
||||
>assertAndPredicate : (x: string | number | Date) => x is string
|
||||
>snd : string | number | Date
|
||||
|
||||
let t: string = snd; // should ok
|
||||
>t : string
|
||||
>snd : string
|
||||
|
||||
} else {
|
||||
snd; // type is number | Date
|
||||
>snd : number | Date
|
||||
}
|
||||
|
||||
|
||||
@@ -239,3 +239,18 @@ declare let unk: unknown;
|
||||
if (isNumOrStr(unk)) {
|
||||
let t: number | string = unk; // should ok
|
||||
}
|
||||
|
||||
// A function can be a type predicate even if it throws.
|
||||
function assertAndPredicate(x: string | number | Date) {
|
||||
if (x instanceof Date) {
|
||||
throw new Error();
|
||||
}
|
||||
return typeof x === 'string';
|
||||
}
|
||||
|
||||
declare let snd: string | number | Date;
|
||||
if (assertAndPredicate(snd)) {
|
||||
let t: string = snd; // should ok
|
||||
} else {
|
||||
snd; // type is number | Date
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user