microsoft / microsoft/TypeScript
Inconsistent type compatibility for a type with a call signature and and index signature
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
TypeScript Version: 2.9.0-dev
Search Terms: index signature intersection
Code
type INOk = {
(): string;
[name: string] : number;
}
type IOk = {
(): string;
} & {
[name: string] : number;
}
declare let val:(() => "") & { foo: number; }
let ok: IOk = val; // This works
let nok: INOk = val; // This does not
Expected behavior:
Both IOk and INOk have the same public structure, they both have a call signature and are indexable, both assign statements should be valid.
Actual behavior:
The second assign statement fails with the message Index signature is missing in type '(() => "") & { foo: number; }'.
Playground Link: link
Related Issues: https://github.com/Microsoft/TypeScript/issues/15300
Notes
Looking at the checker code, it appears that for IOk type compatibility is checked for each constituent of the intersection type, so we will have :
isRelatedTo (typeof val, IOk) =
isRealtedTo(typeof val, () => "")) // == True, since val has a call signature
( // Above equal to
isRelatedTo(() => "", () => "") // == True
||
isRelatedTo({ foo: number; }, () => "") // == False but it's does not matter
)
&&
isRealtedTo(typeof val, { [name: string] : number }) // = True since the { foo: number} part of typeof val has an inferable index (ie isObjectTypeWithInferableIndex( { foo: number} ) will return true)
( // Above equal to
isRelatedTo(() => "", { [name: string] : number }) // == False but does not matter
||
isRelatedTo({ foo: number; }, { [name: string] : number }) // == True,
)
While for INOk the relation is checked directly, since INOk can't be split into constituents and we have
isRelatedTo (typeof val, INOk) =
isRelatedTo(() => "", INOk) // == False INOk has index, but ()=> "" does not
||
isRelatedTo({ foo: number; }, INOk) // == False, No compatible call signature
So then the checker falls back to structural checking (recursiveTypeRelatedTo) but this fails as well because when checking for index compatibility (inside indexTypesRelatedTo), it decides that the typeof value ((() => "") & { foo: number; }) does not have an inferable index (isObjectTypeWithInferableIndex returns false because the intersection type does not have a symbol and even if it did the condition for inferable index checks that the type does not have a call signature (!typeHasCallOrConstructSignatures(type)))
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Reproduziere das Beispiel mit TypeScript 2.9.0-dev oder dem aktuellen Playground und untersuche anschließend die im Issue genannten Checker-Pfade: isRelatedTo, recursiveTypeRelatedTo, indexTypesRelatedTo und isObjectTypeWithInferableIndex. Vergleiche die Kompatibilitätsbehandlung für den Schnittmengentyp und den Typ mit kombinierten Aufruf- und Indexsignaturen; abgeschlossen ist die Aufgabe, wenn beide Zuweisungen konsistent akzeptiert werden, ohne andere Prüfungen zu schwächen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100