microsoft / microsoft/TypeScript

Allow type assertions to consider typed index signatures

Aperta
#30,342 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

In Discussion Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

Search Terms

interface, index, signature, typed, assertion, conversion, partial,

Suggestion

Currently type assertions do not seem to respect typed index signatures [key: string]: any; in interfaces or test for compatibility before attempting to convert.

I am proposing that type assertions would be able to convert this situation without error:

interface Test {
	a: string;
	b?: string;
	[key: string]: any;
}

const a = {b: 'b', c: 3 } as Test;

Use Cases

Currently to work around this I need to first create an intermediate variable that is typed as a Partial before making the assertion. I would prefer to just make the assertion.

Examples

This example above will error because {b: 'b', c: 3 } is missing property a

interface Test {
	a: string;
	b?: string;
	[key: string]: any;
}

const a = {b: 'b', c: 3 } as Test;

playground

If I remove c: 3 from the object then there is no error and a is no longer required:

interface Test {
	a: string;
	b?: string;
	[key: string]: any;
}

const a = {b: 'b' } as Test;

playground

Currently to workaround this an intermediate typed value can be used so the subtype is already confirmed before the assertion:

interface Test {
	a: string;
	b?: string;
	[key: string]: any;
}

const test: Partial<Test> = { b: 'b', c: 3 };
const a = test as Test

playground

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Riproduci gli esempi collegati di Playground, in particolare l'asserzione che coinvolge una firma di indice tipizzata e una proprietà aggiuntiva. Inizia individuando i controlli di compatibilità delle asserzioni di tipo di TypeScript e i relativi test di regressione; il lavoro sarà completato quando l'asserzione proposta avrà esito positivo, mentre le asserzioni incompatibili continueranno a essere rifiutate.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.