-
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: typescript@3.7.0-dev.20190831
Search Terms: void guard strictNullChecks boolean negation refinement
Code
// Toggle strictNullChecks off and see the type error appear
// on line 7, while 2nd fn never errors and 3rd always errors
const getMaybeStringLen = (maybeString: string | void) => {
if (maybeString === null || maybeString === undefined) {
return undefined;
}
return maybeString.length;
};
const getMaybeStringLenBoolNegation = (maybeString: string | void) => {
if (!maybeString) {
return undefined;
}
return maybeString.length;
};
const getMaybeStringLenBoolCoercionNegation = (maybeString: string | void) => {
if (!Boolean(maybeString)) {
return undefined;
}
return maybeString.length;
};Expected behavior:
strictNullChecks: falsebehaves less strictly thanstrictNullChecks: true. Admittedly a loose criterion, but it seems like the stricter check fails withstrictNullChecks: falseand passes withstrictNullChecks: true, while the type coercive check passes in both cases.
My confusion stems from the fact that ! is a valid type guard for void for both compiler flags, while checking against null and undefined is valid for strictNullChecks: true only.
As a bonus, while ! is always valid, !Boolean(x) is never valid.
Actual behavior:
getMaybeStringLen...- ...typechecks with
strictNullChecks: true - ...does NOT typecheck with
strictNullChecks: false
- ...typechecks with
getMaybeStringLenBoolNegation...- ...typechecks with
strictNullChecks: true - ...typechecks with
strictNullChecks: false
- ...typechecks with
getMaybeStringLenBoolCoercionNegation...- ...does NOT typecheck with
strictNullChecks: true - ...does NOT typecheck with
strictNullChecks: false
- ...does NOT typecheck with
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScript