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

Infer type of None #11

Closed
OliverJAsh opened this issue Jul 12, 2016 · 3 comments
Closed

Infer type of None #11

OliverJAsh opened this issue Jul 12, 2016 · 3 comments
Labels

Comments

@OliverJAsh
Copy link

Is it possible to infer the type of None when used alongside Some?

E.g. in Scala:

scala> def x = (a: Int) => if (a > 5) Some(5) else None
x: Int => Option[Int]

In TypeScript:

const x = (a: number) => (a > 5) ? Option(5) : None

x here is of type (a: number) => Option<any>, because None is typed as Option<any>.

I wonder if it's possible to infer the generic type of None here from the usage of Option in the same function?

@jklmli
Copy link
Owner

jklmli commented Jul 22, 2016

This should be possible once TypeScript 2.0 lands (soon!).

None being typed as Option<any> is wrong, because any is the supertype of all types, rather than the subtype of all types.

Typescript 2.0 is getting a never type that is a subtype of all types. This should allow the compiler to inference things as you'd expect.

@OliverJAsh
Copy link
Author

Will this mean you can do the equivalent of def x = (a: Int) => if (a > 5) Some(5) else None? E.g.

x = (a: number): Option<number> => if (a > 5) { Option(5) } else { None }

Currently I have to do

x = (a: number): Option<number> => if (a > 5) { Option(5) } else { None as Option<number> }

@jklmli
Copy link
Owner

jklmli commented Jul 22, 2016

Yes, TypeScript 2.0 should allow the shorter form =)

@jklmli jklmli closed this as completed Aug 22, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants