microsoft / microsoft/TypeScript
Type is not checked when using object spread and computed property of type `number`
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
Bug Report
🔎 Search Terms
spread computed property number reduce
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
⏯ Playground Link
Playground link with relevant code
💻 Code
declare const id: number;
type Acc = { [key: string]: number };
declare const acc: Acc;
// Expected error, but got none. ❌
const acc2: Acc = {
...acc,
[id]: 'invalid',
};
If we change the type of the computed property id from number to string, it works as expected (we get an error).
declare const id: string;
type Acc = { [key: string]: number };
declare const acc: Acc;
// Expected error, and got none. ✅
const acc2: Acc = {
...acc,
[id]: 'invalid',
};
Alternatively, if we remove the spread of acc it works as expected:
declare const id: number;
type Acc = { [key: string]: number };
// declare const acc: Acc;
// Expected error, and got none. ✅
const acc2: Acc = {
[id]: 'invalid',
};
My workaround for now is to convert the type before it's used as a computed property:
declare const id: number;
type Acc = { [key: string]: number };
declare const acc: Acc;
// Expected error, and got none. ✅
const acc2: Acc = {
...acc,
[id.toString()]: 'invalid',
};
🙁 Actual behavior
See code comments above.
🙂 Expected behavior
See code comments above.
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 la riproduzione collegata in TypeScript Playground e confronta i casi usando una proprietà calcolata numerica rispetto a una di tipo string, con e senza l’object spread. Traccia il comportamento del type-checking per il literal dell’oggetto e verifica che il caso della proprietà numerica segnali un errore, mentre gli altri esempi mantengano il comportamento 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
- Specificata chiaramente
- Idoneità per principianti
- 35/100