-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Closed
Labels
DuplicateAn existing issue was already createdAn existing issue was already created
Description
Bug Report
π Search Terms
predicate
type predicate
predicate syntax
π Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about type predicates.
β― Playground Link
Playground link with relevant code
π» Code
function a() {
const validFlags = ['-a', '-b', '-c'] as const;
function isFlagValid(flag: string): flag is typeof validFlags[number] {
return validFlags.includes(flag as typeof validFlags[number]);
}
const flag = Math.random().toString();
if (isFlagValid(flag) === false) {
flag; // string
return;
}
flag; // string
if (!isFlagValid(flag)) {
flag; // string
return;
}
flag; // typeof validFlags[number]
}
function b() {
const validFlags = ['-a', '-b', '-c'] as const;
function isFlagValid(flag: string): flag is typeof validFlags[number] {
return validFlags.includes(flag as typeof validFlags[number]);
}
const flag = Math.random().toString();
if (isFlagValid(flag) === true) {
flag; // string
return;
}
flag; // string
if (isFlagValid(flag)) {
flag; // typeof validFlags[number]
return;
}
flag; // string
}π Actual behavior
The function with a type predicate only limits the type definition of flag if it's used in shorthand syntax !isFlagValid(flag) or isFlagValid(flag) and not the more explicit isFlagValid(flag) === false or isFlagValid(flag) === true.
π Expected behavior
The function with a type predicate limits the type definition of flag regardless of the syntax uses and they both mean the same thing.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
DuplicateAn existing issue was already createdAn existing issue was already created