-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Non-null assertion type operator !
#14366
Comments
This requires a null assertion type operator, as you noted. something of the form Also referenced in #13253 |
!
Related #12636 |
Another dupe: #17370 |
As it seems a special operator for this is no longer planned (see #17948 (comment)), I added an example for this at 4d14c9f now. |
@tycho01 This still possible somehow? Apparently the syntax used in your example is no longer valid. I'm trying to achieve the opposite of The following would be possible if
Is there any alternatives? |
@cvsguimaraes: it's an outstanding PR, it has never been possible yet on |
It's not perfect, but a workaround for the example at the top is to do type Baz = (Foo["bar"] & {"baz": {}})["baz"]; to get the deeper type. it does make the type To simplify that, i made the following helper (called type Getter<T, K extends string> = T & Record<K, {}>;
type Get<T, K extends keyof Getter<T, K>> = Getter<T, K>[K];
// Usage:
type Baz = Get<Foo["bar"], "baz">; |
Fixed in #22094 by tweaking the compiler to allow partial unions for indexed types - therefore not requiring a null assertion operator. |
a |
!
!
Discussed this again in #22445. Conclusion, with |
TypeScript Version: 2.2.1
Code
Expected behavior:
I would expect there to be a way to specify
bar
is non-null, such asFoo["bar!"]
.Actual behavior:
The code doesn't compile with the following error message:
"Property 'baz' does not exist on type '{ baz: number; } | null'."
The text was updated successfully, but these errors were encountered: