microsoft / microsoft/TypeScript
Easier destructuring with type annotations on binding patterns
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
Search Terms
type inference destructuring syntax conflict
Suggestion
It's currently not possible to destructure a value in Typescript and provide types for each of the values due to the syntax clash with destructuring and renaming at the same time. You can see exactly this issue in the Typescript FAQs at: https://github.com/Microsoft/TypeScript/wiki/FAQ#why-cant-i-use-x-in-the-destructuring-function-f-x-number------
This is frustrating when programming in React where it's very common to see this pattern:
const MyComponent = ({ a, b }) => {
// ...
}
But in Typescript a and b are untyped (inferred to have type any) and type annotation must be added (either to aid in type safety or to avoid compiler errors, depending on the state of the user's strict flags). To add the correct type annotation it feels natural to write:
const MyComponent = ({ a : string, b : number }) => {
// ...
}
but that's not what the user thinks due to the aforementioned syntax clash. The only valid syntax in Typescript is actually this:
const MyComponent = ({ a, b } : { a : string, b : number }) => {
// ...
}
Which is very strange to write and difficult to read when the object has more than two parameters or the parameters have longer names. Also the value names have been duplicated -- once in the destructuring and once in the type annotation.
I suggest we allow some other symbol (my current thinking is a double colon) to make the syntax unambiguous in this specific scenario:
const MyComponent = ({ a :: string, b :: number }) => {
// ...
}
Although this is really the only place it would be used, for the sake of consistency, I think is should be allowed everywhere:
const a :: string = "";
const b :: number = 1;
Use Cases
It would allow for type-safe destructuring of values where the type cannot be inferred by the compiler (such as function parameters).
Examples
A good example of the sort of React components I'm talking about (and one of the first Google results for React Functional Components) can be found at https://hackernoon.com/react-stateless-functional-components-nine-wins-you-might-have-overlooked-997b0d933dbc. We can use my proposed syntax in the functional component definition:
import React from 'react'
const HelloWorld = ({name :: string}) => {
const sayHi = (event) => {
alert(`Hi ${name}`)
}
return (
<div>
<a href="#"
onclick={sayHi}>Say Hi</a>
</div>
)
}
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
Beginne mit der Überprüfung des im Issue verlinkten TypeScript-FAQ-Abschnitts und der bestehenden Behandlung von binding-pattern annotations. Vergleiche die vorgeschlagene Syntax mit aktuellen Destructuring-Beispielen und bestimme, wie sich das Feature für Funktionsparameter und gewöhnliche Variablendeklarationen verhalten sollte; im Issue werden keine Implementierungsdateien oder Tests genannt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 25/100