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

Non-null assertion type operator ! #14366

Closed
YosubShin opened this issue Feb 28, 2017 · 10 comments
Closed

Non-null assertion type operator ! #14366

YosubShin opened this issue Feb 28, 2017 · 10 comments
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript

Comments

@YosubShin
Copy link

TypeScript Version: 2.2.1

Code

interface Foo {
    bar: {
        baz: number;
    } | null;
}

type Baz = Foo["bar"]["baz"];  // This doesn't compile with the error: "Property 'baz' does not exist on type '{ baz: number; } | null'."
let x: Baz = 0;

Expected behavior:
I would expect there to be a way to specify bar is non-null, such as Foo["bar!"].

Actual behavior:
The code doesn't compile with the following error message:
"Property 'baz' does not exist on type '{ baz: number; } | null'."

@mhegazy
Copy link
Contributor

mhegazy commented Mar 2, 2017

This requires a null assertion type operator, as you noted. something of the form type Baz = (Foo["bar"])!["baz"];.

Also referenced in #13253

@mhegazy mhegazy added the Suggestion An idea for TypeScript label Mar 2, 2017
@mhegazy mhegazy changed the title Lookup Types lookup fails for nullable types when "strictNullChecks" is on Assertion type operator ! Mar 2, 2017
@mhegazy mhegazy changed the title Assertion type operator ! Null assertion type operator ! Mar 2, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Mar 3, 2017

Related #12636

@KiaraGrouwstra
Copy link
Contributor

Another dupe: #17370

@KiaraGrouwstra
Copy link
Contributor

As it seems a special operator for this is no longer planned (see #17948 (comment)), I added an example for this at 4d14c9f now.

@kbtz
Copy link

kbtz commented Nov 3, 2017

@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 or Assert<T> were available:

/**
 * Make all properties in T required
 */
type Whole<T> = {
    [P in keyof T]!: T[P];
};

Is there any alternatives?

@KiaraGrouwstra
Copy link
Contributor

@cvsguimaraes: it's an outstanding PR, it has never been possible yet on master. The current topic is just a sub-case of #4183.

@forivall
Copy link

forivall commented Jan 19, 2018

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 number & {} but it at least makes the above code work

To simplify that, i made the following helper (called Get because 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">;

@nomcopter
Copy link

Fixed in #22094 by tweaking the compiler to allow partial unions for indexed types - therefore not requiring a null assertion operator.

@KiaraGrouwstra
Copy link
Contributor

a NonNullable type was part of #21847

@DanielRosenwasser DanielRosenwasser changed the title Null assertion type operator ! Non-null assertion type operator ! Mar 1, 2018
@mhegazy
Copy link
Contributor

mhegazy commented Mar 9, 2018

Discussed this again in #22445. Conclusion, with NonNullable available now with conditional types, the original scenario should be unblocked, no reason to add new rules.

@mhegazy mhegazy closed this as completed Mar 9, 2018
@mhegazy mhegazy added the Declined The issue was declined as something which matches the TypeScript vision label Mar 9, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

7 participants