microsoft / microsoft/TypeScript
Optional fields affecting keyof operator in generic class decorators
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
TypeScript Version: 3.2.2
Search Terms:
class decorator keyof typescript
Code
function View<T>(name: string, fields: (keyof T)[]) {
return (target: new(...args: any[]) => T) => {
// Do something with fields
};
}
// works
@View('active', ['street1', 'street2'])
class Address {
street1: string;
street2: string;
}
// broken
@View('active', ['street1', 'street2'])
class AddressBroken {
street1: string;
street2?: string;
}
Expected behavior:
Both scenarios should work above.
Actual behavior:
It appears that optional fields are causing the decorator to create invalid types.
Argument of type 'typeof AddressBroken' is not assignable to parameter of type 'new (...args: any[]) => { street1: any; street2: any; }'.
Type 'AddressBroken' is not assignable to type '{ street1: any; street2: any; }'.
Property 'street2' is optional in type 'AddressBroken' but required in type '{ street1: any; street2: any; }'.
1234
Playground Link:
Playground Sample
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 l’esempio Playground collegato e riproduci l’errore del decorator relativo a keyof, alle classi generiche e a un campo opzionale. Traccia la gestione da parte del compilatore del tipo di destinazione del decorator, quindi aggiungi un test di regressione che includa sia Address sia AddressBroken e verifica che entrambi gli esempi vengano compilati senza l’errore segnalato.
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