microsoft / microsoft/TypeScript
Allow type assertions to consider typed index signatures
Personne n'a encore pris cette issue.
- 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;
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;
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
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
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- 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