microsoft / microsoft/TypeScript
Allow type assertions to consider typed index signatures
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 2 T. 4 Std.
- Gemergte PRs (30 T.)
- 132
Beschreibung
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.
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 die verknüpften Playground-Beispiele, insbesondere die Assertion, die eine typisierte Index-Signatur und eine zusätzliche Eigenschaft betrifft. Beginne damit, die Kompatibilitätsprüfungen für TypeScript-Typassertionen und deren Regressionstests zu finden; fertig ist die Aufgabe, wenn die vorgeschlagene Assertion erfolgreich ist, während inkompatible Assertions weiterhin abgelehnt werden.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 45/100