microsoft / microsoft/TypeScript
Generic derived value type
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
Hey guys.
I can not see if I'm doing anything wrong here. I am expecting to be able to get exact type from generic type ...
Is this just limitation of typescript or i am doing anything whrong here?
TypeScript Version: 3.1.3
Search Terms:
Generic derived value type
Code
export type filterTypeName = 'price' | 'price_range';
export type priceFilterValue = {
from: number;
to: number;
};
export type priceRangeFilterValue = {
fromRange: number;
toRange: number;
};
export type filterValueType<T extends filterTypeName> =
T extends 'price' ? priceFilterValue
: T extends 'price_range' ? priceRangeFilterValue : never;
type productFilterParameters<T extends filterTypeName> = {
filter: T;
value: filterValueType<T>;
};
function addProductFilter<T extends filterTypeName>(params: productFilterParameters<T>) {
switch (params.filter) {
case 'price':
const priceFrom = params.value.from; //Property 'from' does not exist on type 'filterValueType<T>'
break;
default:
break;
}
}
// parameter value is derivered based on filter type... not working id function definition
addProductFilter({ filter: 'price', value: { from: 1, to: 2 } });
//expected behavior something like this
type productFilterParametersCorrect = {
filter: 'price';
value: priceFilterValue;
} | {
filter: 'price_range';
value: priceRangeFilterValue;
}
function addProductFilterCorrect(params: productFilterParametersCorrect) {
switch (params.filter) {
case 'price':
const priceFrom = params.value.from; // correct
break;
case 'price_range':
const priceRangeFrom = params.value.fromRange; // correct
default:
break;
}
}
Expected behavior:
Code
const value: priceFilterValue
Actual behavior:
Code
const value: filterValueType<T>
Playground Link:
Playground
Related Issues:
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
Inizia con il Playground collegato e con l’esempio generico addProductFilter usando TypeScript 3.1.3, quindi confronta il comportamento del parametro generico con l’unione definita esplicitamente. Il lavoro è completato quando lo switch su params.filter fornisce le proprietà dei valori corrispondenti senza l’errore segnalato; il payload non menziona file o test del repository.
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