microsoft / microsoft/TypeScript
Generics return type of function reported not assignable.
Open
Nobody has claimed this yet.
Bug
Domain: check: Type Inference
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
Bug Report
🔎 Search Terms
return value, typescript, generics
🕗 Version & Regression Information
This changed between versions 3.5.1 and 3.6.3
⏯ Playground Link
💻 Code
type TypeCaseA<A> = { data: A };
type TypeCaseB<B = string> = { data?: B; code: string; };
type TypeCaseAB<A, B = string> = TypeCaseA<A> | TypeCaseB<B>;
class Service {
getB<B = string>(code: string, data?: B): TypeCaseB<B> {
const result: TypeCaseB<B> = { data, code };
return result;
}
getA<A>(data: A): TypeCaseA<A> {
return { data };
}
func1(): TypeCaseAB<{ id: number }> {
/* The following line is blamed with:
Type 'TypeCaseB<string | { id: number; }>' is not assignable to type 'TypeCaseAB<{ id: number; }, string>'.
Type 'TypeCaseB<string | { id: number; }>' is not assignable to type 'TypeCaseB<string>'.
Type 'string | { id: number; }' is not assignable to type 'string'.
Type '{ id: number; }' is not assignable to type 'string'.
*/
return this.getB('1231');
}
func2(): TypeCaseAB<{ id: number }> {
const retval = this.getB('1231');
return retval;
}
func3(): TypeCaseAB<{ id: number }, string> {
return this.getB('1231', '456');
}
}
🙁 Actual behavior
func1 is tested with the following error:
Type 'TypeCaseB<string | { id: number; }>' is not assignable to type 'TypeCaseAB<{ id: number; }, string>'.
Type 'TypeCaseB<string | { id: number; }>' is not assignable to type 'TypeCaseB<string>'.
Type 'string | { id: number; }' is not assignable to type 'string'.
Type '{ id: number; }' is not assignable to type 'string'.
There should be no difference from func2 which is tested correct.
🙂 Expected behavior
No error reported.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the diagnostic in the linked TypeScript Playground using the TypeCaseA, TypeCaseB, TypeCaseAB, and Service.func1/func2/func3 examples. Compare the generic return-type handling in func1 with the accepted func2 and func3 cases. Done means func1 produces no error while preserving the expected TypeCaseAB return type.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100