-
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 property in map type #19608
Comments
Maybe related to #14366 |
Took a minute to understand #14366 but yes, I think that's basically the same thing. |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
This should be doable today using |
@mhegazy Would appreciate some insight into how this should work... I tried using interface User {
id?: string;
name?: string;
}
type CompleteUser = {
[P in keyof User]: NonNullable<User[P]>;
}
function isCompleteUser(user: User): user is CompleteUser {
return user.id != null && user.name != null;
}
declare const user: User;
if (isCompleteUser(user)) {
user.id // still string | undefined
user.name // still string | undefined
} How should this work? |
Sorry for this use case, you can just use the issue is sorry for the wrong suggestion earlier. |
@mhegazy Thanks! I somehow missed the new subtractive mapped type modifier syntax, that does exactly what I was asking here (and more)! |
TypeScript Version: 2.7.0-dev.201xxxxx
Feature request
It would be helpful when using
strictNullChecks
to have some way to map a type with optional fields to an equivalent type with non-nullable fields. I would expect using the!
non-null assertion like syntax somewhere in a map type:Why I want this
I've found that after turning on
strictNullChecks
I run into a lot of data types that have optional fields (in particular return values from http requests where all fields could be missing) that end up producing a lot of false flag "possibly null" errors in down-stream code. The problem is that even though I validate data at a high level, the data type holds onto the "possibly undefined" type wherever the data type is used in down-stream code, for example in my React component tree props. The truth is many of these components only get rendered when those fields are populated, but TS doesn't know this and probably couldn't possibly know this.So the obvious solution is to create mirrors of the original data types that have certain fields defined as non-null... but this becomes quite cumbersome and leaky to maintain, especially since you rightfully can't assign something that has an optional field to something that doesn't.
In the end what I'm really trying to express is a type derived from a type with
null | undefined
removed from certain (or all) fields. Basically the opposite ofPartial<>
. And if I can do this generically I can eliminate a lot of the maintenance cost.Code
The text was updated successfully, but these errors were encountered: