-
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
Way to specify that type can't be null #14967
Comments
You can try f<T extends {}>(x: Array) |
@Pajn that is already supported declare function filterMap<T, U>(data: Array<T>, fn: (element: T) => U | null): Array<U>; |
@aluanhaddad |
I'm not sure I follow you. When you say explicitley pass a nullable type for filterMap(fruits, e => null)) or filterMap<string, string | null>(fruits, e => null)) If so, you can write overload such as declare function filterMap<T, U extends null>(data: T[], fn: (element: T) => U): [never];
declare function filterMap<T, U>(data: T[], fn: (element: T) => U | null): U[]; |
I have wanted this a couple of times the last days with the use case of taking property from another type but requiring it. Example type A = {prop?: string}
type B = {prop: A['prop']} // type of B is {prop: string|undefined}
// I would like
type B = {prop: A['prop']!} // type of B is {prop: string} |
Duplicate of #14366 |
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 possible now with |
TypeScript Version: 2.2.1
Code
With the code
filteredAndMapped
now have the typeArray<string | null>
but in reality only contain elements of type string.It would be nice if it was possible to do a non-null assertion or similar for types, allowing me to write something similar to
so that
filteredAndMapped
andnewArray
have the typeArray<string>
.The text was updated successfully, but these errors were encountered: