microsoft / microsoft/TypeScript

Allow type assertions to consider typed index signatures

Ouverte
#30,342 2 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

In Discussion Suggestion
Langage dominant
Go
Étoiles
111k
Forks
14.3k
Merge moyen
1 j 19 h
PR mergées (30 j)
117

Description

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.

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Reproduisez les exemples liés de Playground, en particulier l’assertion impliquant une signature d’index typée et une propriété supplémentaire. Commencez par localiser les vérifications de compatibilité des assertions de type de TypeScript et leurs tests de régression ; le travail sera terminé lorsque l’assertion proposée réussira, tandis que les assertions incompatibles continueront d’être rejetées.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
typescript
Domaine
compilers
Type d'issue
Fonctionnalité
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
45/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.