-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Closed
Labels
BugA bug in TypeScriptA bug in TypeScript
Milestone
Description
TypeScript Version: 3.9.2
Search Terms: some combination of subtype, narrowing, type assertion, undefined, null, empty string, empty tuple
Code
declare function isEmptyString(value: string): value is '';
declare function isMaybeEmptyString(value: string | null | undefined): value is '' | null | undefined;
declare function isZero(value: number): value is 0;
declare function isMaybeZero(value: number | null | undefined): value is 0 | null | undefined;
declare function isEmptyArray<T>(value: T[]): value is [];
declare function isMaybeEmptyArray<T>(value: T[] | null | undefined): value is [] | null | undefined;
const TEST_CASES = [
(value: string) => {
if (isEmptyString(value)) {
value // ""
} else {
value // string
}
if (isMaybeEmptyString(value)) {
value // ""
} else {
value // string
}
},
(value?: string) => {
if (isMaybeEmptyString(value)) {
value // EXPECTED '' | undefined ; ACTUAL undefined
} else {
value // string
}
},
(value: number) => {
if (isZero(value)) {
value // 0
} else {
value // number
}
if (isMaybeZero(value)) {
value // 0
} else {
value // number
}
},
(value?: number) => {
if (isMaybeZero(value)) {
value // EXPECTED 0 | undefined ; ACTUAL undefined
} else {
value // number
}
},
(value: string[]) => {
if (isEmptyArray(value)) {
value // []
} else {
value // string[]
}
if (isMaybeEmptyArray(value)) {
value // EXPECTED [] ; ACTUAL string[] & [] (GOOD ENOUGH but why is it different than the above?)
} else {
value // string[]
}
},
(value?: string[]) => {
if (isMaybeEmptyArray(value)) {
value // EXPECTED [] | undefined ; ACTUAL undefined
} else {
value // string[]
}
},
];
Expected behavior: (inlined)
Actual behavior: (inlined)
TL;DR the type system is apparently expressive enough to express this, until null
/undefined
got involved
Related Issues:
Maybe same as #31156, not 100% sure
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScript