microsoft / microsoft/TypeScript
Need way to express hybrid types that are indexable for a subset of properties
Personne n'a encore pris cette issue.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.3k
- Merge moyen
- 2 j 4 h
- PR mergées (30 j)
- 132
Description
Edit by @DanielRosenwasser: This might be thought of as a "rest index signature" or a catch-all index signature.
This is a feature request.
TypeScript Version: 2.4
Code
interface CSSProperties {
marginLeft?: string | number
[key: string]: CSSProperties
}
Based on the docs, this is not allowed:
While string index signatures are a powerful way to describe the “dictionary” pattern, they also enforce that all properties match their return type. This is because a string index declares that obj.property is also available as obj[“property”]. In the following example, name’s type does not match the string index’s type, and the type-checker gives an error:
Unfortunately, it seems to make this type (which is common amongst jss-in-css solutions) not expressible. Coming from flow, which handles index types by assuming they refer to the properties that are not explicitly typed, this is frustrating.
As it stands, you can workaround it with:
interface CSSProperties {
marginLeft?: string | number
[key: string]: CSSProperties | string | number
}
But this is not sound. It allows this:
const style: CSSProperties = {
margarineLeft: 3
}
This could potentially be solved with subtraction types if they allowed subtracting from string (and you were allowed to specify key types in this way):
interface CSSProperties {
marginLeft?: string | number
}
interface NestedCSSProperties extends CSSProperties {
[key: string - keyof CSSProperties]: CSSProperties
}
I asked about this on stackoverflow to confirm that I wasn't missing something. It seems I'm not, so I guess I'd consider this a suggestion/discussion starter, since it's probably not a "bug". Thanks!
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
Commencez par les exemples de CSSProperties et les directives du handbook liées, puis reproduisez le comportement actuel du type-checker pour des propriétés explicites combinées à une index signature. La tâche sera terminée lorsque le type hybride demandé pourra être exprimé tout en rejetant les propriétés inconnues dont les types sont incompatibles, sans dépendre du contournement de union unsound.
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é
- 5/5
- Temps estimé
- Plus d'une semaine
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 28/100