-
Notifications
You must be signed in to change notification settings - Fork 13k
Closed
Labels
BugA bug in TypeScriptA bug in TypeScript
Milestone
Description
TypeScript Version: [email protected]
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: false
behaves less strictly thanstrictNullChecks: true
. Admittedly a loose criterion, but it seems like the stricter check fails withstrictNullChecks: false
and 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