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

Way to specify that type can't be null #14967

Closed
Pajn opened this issue Apr 1, 2017 · 8 comments
Closed

Way to specify that type can't be null #14967

Pajn opened this issue Apr 1, 2017 · 8 comments
Labels
Duplicate An existing issue was already created

Comments

@Pajn
Copy link

Pajn commented Apr 1, 2017

TypeScript Version: 2.2.1

Code

With the code

function filterMap<T, U>(data: Array<T>, fn: (element: T) => U): Array<U> {
  let newArray: Array<U> = []
  data.forEach(element => {
    let newElement = fn(element)
    if (newElement != null) {
      newArray.push(newElement)
    }
  })
  return newArray
}

let fruits = ['apple', 'banana', 'pear']
let filteredAndMapped = filterMap(fruits, e => e.startsWith('a') ? null : e.toUpperCase())

filteredAndMapped now have the type Array<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

function filterMap<T, U>(data: Array<T>, fn: (element: T) => U): Array<U!> {
  let newArray: Array<U!> = []
  // ...

so that filteredAndMapped and newArray have the type Array<string>.

@NN---
Copy link

NN--- commented Apr 1, 2017

You can try f<T extends {}>(x: Array)

@aluanhaddad
Copy link
Contributor

@Pajn that is already supported

declare function filterMap<T, U>(data: Array<T>, fn: (element: T) => U | null): Array<U>;

@Pajn
Copy link
Author

Pajn commented Apr 2, 2017

@aluanhaddad
That works if the types are inferred (which is great!) but if you explicitly pass a nullable type into U it doesn't. It would be nice to be able to do that to. Especially as you must specify the type parameters when using a type in a type bound and thus can not rely on that trick inside a type.

@aluanhaddad
Copy link
Contributor

I'm not sure I follow you. When you say explicitley pass a nullable type for U do you mean

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[];

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label May 24, 2017
@Pajn
Copy link
Author

Pajn commented Nov 29, 2017

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}

@mhegazy
Copy link
Contributor

mhegazy commented Nov 29, 2017

Duplicate of #14366

@mhegazy mhegazy marked this as a duplicate of #14366 Nov 29, 2017
@mhegazy mhegazy added Duplicate An existing issue was already created and removed Needs Investigation This issue needs a team member to investigate its status. labels Nov 29, 2017
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@mhegazy
Copy link
Contributor

mhegazy commented Mar 2, 2018

This should be possible now with NonNullable<A["prop"]>. see #21847

@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

6 participants