Skip to content
New issue

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

add rule to disallow non-null assertion after optional chain #749

Open
ajafff opened this issue Jan 17, 2021 · 0 comments
Open

add rule to disallow non-null assertion after optional chain #749

ajafff opened this issue Jan 17, 2021 · 0 comments

Comments

@ajafff
Copy link
Member

ajafff commented Jan 17, 2021

declare let obj: {prop: string | null} | undefined;

let a = obj?.prop!; // not allowed: 'a' is inferred as 'string' instead of 'string | undefined'
let b = obj?.prop!.length; // allowed to asssert that a certain property in the chain is not nullish, 'b' is 'number | undefined' as expected

NonNull inside an optional chain is treated special and only asserts that one element in the chain is non-null. Whereas NonNull at the end of an optional chain removes null and undefined from the type of the whole chain. That doesn't make any sense because you wouldn't need an optional chain if you knew everything is non-null.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant