I have a code that works in angular 2, but when I try to use it in a nativescript project fails. I´m trying to reject a promise like this:
login(credentials:Credentials):Promise<User> {
if (!valid) {
return Promise.reject<User>("Invalid password");
}else {
return Promise.resolve(new User("some user"));
}
}
And I get this error:
Error:(32, 22) TS2346: Supplied parameters do not match any signature of call target.
You missed to return promise when you Rejected it.
Error directly says that you aren't returning
Promise<User>
from function.
As method return type is
Promise<User>
, it always return that object.
PS: After edit in OP found that method can return two types of data, on success it would be User
object, on reject its string
. So for such case I'd prefer you to change the method return type to User | string
User | string
Code
login(credentials:Credentials):Promise<User | string> {
if (!valid) {
//returned promise here which was missing and failing compilation
return Promise.reject("Invalid password");
}else {
return Promise.resolve(new User("some user"));
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.