Repository navigation
Non-null assertion type operator ! #14366
Description
Activity
This requires a null assertion type operator, as you noted. something of the form
type Baz = (Foo["bar"])!["baz"];.Also referenced in #13253
- changed the title
[-]Lookup Types lookup fails for nullable types when "strictNullChecks" is on[/-][+]Assertion type operator `!`[/+]on Mar 2, 2017 - changed the title
[-]Assertion type operator `!`[/-][+]Null assertion type operator `!`[/+]on Mar 2, 2017 Related #12636
- addedDuplicateAn existing issue was already createdAn existing issue was already createdand removedDuplicateAn existing issue was already createdAn existing issue was already created
on Apr 13, 2017 KiaraGrouwstra commented
on Aug 12, 2017 ContributorMore actionsAnother 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
Partial<T>to describe initialized (i.e defaults applied) to option objects (a mix of optional and required properties).The following would be possible if
!operator orAssert<T>were available:/** * Make all properties in T required */ type Whole<T> = { [P in keyof T]!: T[P]; };Is there any alternatives?
Reacted by Kristian Hanekampcvsguimaraes: it's an outstanding PR, it has never been possible yet on
master. The current topic is just a sub-case of #4183.Reacted by bykbtzrReacted by bykbtzrIt'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
number & {}but it at least makes the above code workTo simplify that, i made the following helper (called
Getbecause of_.get):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
NonNullabletype was part of #21847- changed the title
[-]Null assertion type operator `!`[/-][+]Non-null assertion type operator `!`[/+]on Mar 1, 2018 Discussed this again in #22445. Conclusion, with
NonNullableavailable now with conditional types, the original scenario should be unblocked, no reason to add new rules.Reacted by kiara- addedDeclinedThe issue was declined as something which matches the TypeScript visionThe issue was declined as something which matches the TypeScript vision
on Mar 9, 2018 - locked and limited conversation to collaborators
on Jul 25, 2018
TypeScript Version: 2.2.1
Code
Expected behavior:
I would expect there to be a way to specify
baris 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'."