microsoft / microsoft/TypeScript
Generics return type of function reported not assignable.
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
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.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Riprodurre la diagnostica nel TypeScript Playground collegato utilizzando gli esempi TypeCaseA, TypeCaseB, TypeCaseAB e Service.func1/func2/func3. Confrontare la gestione del tipo restituito generico in func1 con i casi accettati func2 e func3. Il lavoro è completato quando func1 non produce errori e preserva il tipo restituito TypeCaseAB previsto.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100