-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
SUGGESTION: Assert return type is not undefined or null #15100
Comments
So essentially you want a type |
it's like this: #15098 with testEventSubScribers.getNotifier('foo')?(2);
testEventSubScribers.getNotifier('baz')?('lala'); |
@andy-ms, yes. @dardino, not quite. My code is already ensuring that it is not undefined I don't want users of the code to have to handle (again) the null/undefined cases. |
Sounds like a duplicate of #14366. |
@DanielRosenwasser yes, I think it might be. It's not immediately obvious but yes. I might still have a draft on my tablet at home about a more generic suggestion of a 'strip' operator. I was going to suggest that for a more generic solution we could do something like: type Foo = Date | string | number | null;
type Bar = Foo ^ Date ^ null; // equivalent to: type Bar = string | number Although I'm not sure how much scope or requirement there is for such a thing. |
Here's what I had written In generic terms, to provoke some thought (if easier to implement), I'm after something that can strip a type from an existing compound type. Given we have // This works
type Thing = string | number | Date | null;
type OtherThing = Thing ^ Date; // type OtherThing = string | number | null;
// This would still work too
type AnotherThing = string | number | null; // no Date
type AlienThing = AnotherThing ^ Date; // type AlienThing = string | number | null; |
Subtraction types have already been proposed several times. See #4183 |
Right then. Closing and subscribing to those issues. Thanks everyone. |
TypeScript Version: 2.3.0-dev.20170407
Code
Paste in the following code and scroll to the final two lines:
To remove the errors I need to assert that the result of the getNotifier call is not undefined.
Desired behavior:
It'd be nice if I could declare getNotifier as follows (note the final
!
):The text was updated successfully, but these errors were encountered: