We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Playground link with relevant code
const map = new Map<string, number>(); map.set('a', 1); const a = map.get('a'); const isAValid = Boolean(a); if (!isAValid) { throw Error('no a'); } const aCopy: number = a; if (!a) { throw Error('no a') } const aCopy2: number = a;
In the first example aCopy gives error because it can be undefined according to typescript
aCopy should be defined to be a number.
The text was updated successfully, but these errors were encountered:
We don't really understand that Boolean is testing for truthiness. If you change your code to use !!, you do get the desired result.
Boolean
!!
declare const a: number | undefined; const isAValid = !!a; // <- here if (!isAValid) { throw Error('no a'); } const aCopy: number = a;
If you really do want to use Boolean like this, I guess you could add a signature with a type predicate like this to the global state:
interface BooleanConstructor { <T>(x: T): x is Exclude<T, null | undefined | 0 | "" | false>; }
Sorry, something went wrong.
#16655
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.
No branches or pull requests
Bug Report
π Search Terms
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
In the first example aCopy gives error because it can be undefined according to typescript
π Expected behavior
aCopy should be defined to be a number.
The text was updated successfully, but these errors were encountered: