We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
Is it possible to infer the type of None when used alongside Some?
None
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>.
x
(a: number) => Option<any>
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?
Option
The text was updated successfully, but these errors were encountered:
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.
any
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.
never
Sorry, something went wrong.
Will this mean you can do the equivalent of def x = (a: Int) => if (a > 5) Some(5) else None? E.g.
def x = (a: Int) => if (a > 5) Some(5) else None
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> }
Yes, TypeScript 2.0 should allow the shorter form =)
No branches or pull requests
Is it possible to infer the type of
None
when used alongsideSome
?E.g. in Scala:
In TypeScript:
x
here is of type(a: number) => Option<any>
, because None is typed asOption<any>
.I wonder if it's possible to infer the generic type of
None
here from the usage ofOption
in the same function?The text was updated successfully, but these errors were encountered: