You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In this code sample we can't just write new Ok(user.token) because TS can't infer AuthError type from function's return type. Do you have any plans to add this functionality? Is think that it is not even a breaking change.
src/app/api/mod.ts(16,20): error TS2322: Type 'Ok<CurrentUser, {}>' is not assignable to type 'Result<CurrentUser, AuthError>'.
Types of property 'map' are incompatible.
Type '<U>(fn: (a: CurrentUser) => U) => Result<U, {}>' is not assignable to type '<U>(fn: (a: CurrentUser) => U) => Result<U, AuthError>'.
Type 'Result<any, {}>' is not assignable to type 'Result<any, AuthError>'.
Type '{}' is not assignable to type 'AuthError'.
It can infer AuthError type for second type argument from the function's return type and this is what i'm asking about.
When we have some generic type with missed type arguments, we leave it as is until a first usage of this type in a context where these type arguments is known. Then we can substitute these types and use them as if they were pre-specified.
If I understand the Result type correctly, it seems that the exception type E of one operation isn't really connected to the next operation. You define andThen like this:
Anders Hejlsberg (@ahejlsberg) Result and Option are the types that replace null pointers in Rust. They are very helpful in TS too for using instead of null.
You could then simply remove the return type annotation and rely on inference:
static getApiToken() {
return this.getCurrentUser().andThen(user => new Ok<Token, AuthError>(user.token));
}
You are right, I can do it the way you suggested, but there are several disadvantages:
I need to scan the whole function to find out the return type.
I can't return an interface, only the implementation (without specifying the function return type explicitly).
If I will have several exit points from my function, I will need to repeat these type annotations again and again. This is the major argument. Please take a look at the example:
// Without type inferencefunctionsqrt(x: number): Result<number,string>{if(x<0){returnnewErr<number,string>("Negative square root")}else{returnnewOk<number,string>(Math.sqrt(x))}}// With type inferencefunctionsqrt(x: number): Result<number,string>{if(x<0){returnnewErr("Negative square root")}else{returnnewOk(Math.sqrt(x))}}
There are a lot of other cases where one can use this kind of type inference, e.g.
declarefunctionrequest<T>(url: string,options: any): Promise<T>;/* * Without inference */// All is clear and readable, but we doubling the type annotations.functionloadCurrentUser(): Promise<CurrentUserReply>{returnrequest<CurrentUserReply>('/api/user/current',{/* .. */})}// I have to remember the `request`'s return type all the time.functionloadCurrentUser(){returnrequest<CurrentUserReply>('/api/user/current',{/* .. */})}/* * With inference */// The return type is clear and there are no doubling.functionloadCurrentUser(): Promise<CurrentUserReply>{returnrequest('/api/user/current',{/* .. */})}
I have one more example with Maybe/Option type that shows that usage of generics will be simpler:
functionsayHello(name: Option<string>){if(name.isSome()){alert("Hello "+name.unwrap())}else{alert("Hello, Anonymous")}}// I can't write this waysayHello(newNone());// error TS2345: Argument of type 'None<{}>' // is not assignable to parameter of type 'Option<string>'.// I have to to write this:sayHello(newNone<string>());
There can be a very complex type (maybe generic) instead of string and I have to repeat it again and again in my code. I understand that this feature is not high-priority for TS, but it will be nice to see it in future versions.
Has there been any work done on this? It would be extremely useful to have this kind of inference as part of the language; I stumbled upon this exact problem tonight.
Activity
ahejlsberg commentedon Nov 19, 2014
What is the exact type of the
getCurrentUsermethod? TheandThenmethod is declared to simply propagate theEof theResultinstance it is invoked on.s-panferov commentedon Nov 19, 2014
Anders Hejlsberg (@ahejlsberg) let's move to more simple example, just:
Compiler gives me the error:
It can infer
AuthErrortype for second type argument from the function's return type and this is what i'm asking about.s-panferov commentedon Nov 19, 2014
Saying more formally, I suggest this:
When we have some generic type with missed type arguments, we leave it as is until a first usage of this type in a context where these type arguments is known. Then we can substitute these types and use them as if they were pre-specified.
ahejlsberg commentedon Nov 19, 2014
If I understand the
Resulttype correctly, it seems that the exception typeEof one operation isn't really connected to the next operation. You defineandThenlike this:which implies that the
andThenoperation has the sameEas the result it is invoked on. But shouldn't it instead be:You could then simply remove the return type annotation and rely on inference:
DanielRosenwasser commentedon Nov 19, 2014
Stanislav Panferov (@s-panferov) I've left some feedback on your gist, have a look.
s-panferov commentedon Nov 19, 2014
Daniel Rosenwasser (@DanielRosenwasser) thanks a lot! I will fix it!
Anders Hejlsberg (@ahejlsberg) Result and Option are the types that replace null pointers in Rust. They are very helpful in TS too for using instead of
null.You are right, I can do it the way you suggested, but there are several disadvantages:
s-panferov commentedon Nov 19, 2014
There are a lot of other cases where one can use this kind of type inference, e.g.
s-panferov commentedon Nov 25, 2014
I have one more example with Maybe/Option type that shows that usage of generics will be simpler:
There can be a very complex type (maybe generic) instead of
stringand I have to repeat it again and again in my code. I understand that this feature is not high-priority for TS, but it will be nice to see it in future versions.hmaurer commentedon Dec 14, 2016
Has there been any work done on this? It would be extremely useful to have this kind of inference as part of the language; I stumbled upon this exact problem tonight.
mhegazy commentedon Dec 14, 2016
Looking at this again, this is the same as #11152.