Snippet
getToken(authCode: string): Promise<Token> {
return fetch(tokenUrl, { method: "POST" }).then(res => res.json()).then(json => {
if (json["error"]) {
return Promise.reject(json);
}
return new Token(json);
});
}
Issue reported by Tsc(2.0.6):
xxx.ts(135,81): error TS2345: Argument of type '(json: any) => Promise<never> | Token' is not assignable to parameter of type '(value: any) > PromiseLike<never>'.
Type 'Promise<never> | Token' is not assignable to type 'PromiseLike<never>'.
Type 'Token' is not assignable to type 'PromiseLike<never>'.
Property 'then' is missing in type 'Token'.
Configuration in Tsconfig.json:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"lib": [
"es6",
"dom"
]
}
}
Note about promise typings being defined in
node_modules/typescript/lib/lib.es2015.iterable.d.ts
However, if the rejected Promise is not returned:
getToken(authCode: string): Promise<Token> {
return fetch(tokenUrl, { method: "POST" }).then(res => res.json()).then(json => {
return new Token(json);
});
}
No errors occur in this case. How can a rejected promise be properly returned?