You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've observed a lot of discussion around the verbosity/duplication involved when providing type annotations for destructured object properties (particularly re-ignited by this Svelte issue), which prompted me to wonder if this could be resolved with additional syntax being introduced to TypeScript.
The proposal suggests introducing new syntax for adding inline type annotations using angle brackets <T> within destructuring assignments that can be added immediately after the property keys, along with a new optional modifier keyword to indicate a property as optional.
e.g. this
// beforefunctionfetchData({
url,
method ="GET",
timeout =0}: {url: string;method?: string;timeout?: number;}){console.log(`Fetching data from ${url} with method ${method} and timeout ${timeout}`);}// afterfunctionfetchData({url<string>,method<optionalstring>="GET",timeout<optionalnumber>=0}){console.log(`Fetching data from ${url} with method ${method} and timeout ${timeout}`);}
For completeness (though far less valuable), this could probably also support providing annotations in array destructuring assignments as well. e.g.
// beforefunctionprocessCoordinates([x,y,z]: [number,number,number]){console.log(`Coordinates are x: ${x}, y: ${y}, z: ${z}`);}// afterfunctionprocessCoordinates([x<number>,y<number>,z<number>]){
console.log(`Coordinates are x: ${x}, y: ${y}, z: ${z}`);}
Advantages:
Readability when destructuring is improved as the types are colocated directly next to each of the destructured properties.
Less verbose/redundancy as you don't have to declare destructured properties twice.
Drawbacks:
The angle bracket syntax might introduce more confusion to understanding TypeScript syntax, given it is already being used for both generics and type casting.
This is a non-backwards compatible breaking change to TypeScript.
Other considerations:
Should the optional modifier keyword also work inside of object literal types.
Could a ? be used instead of the new optional modifier inside/outside of the brackets (e.g. url?<string> or url<string?>). Would probably be more familiar to the way optional properties are defined now, but it might get confusing whether ? is part of the property name in the first example, or part of conditional logic in the second.
📃 Motivating Example
Drawing inspiration from the Svelte $props issue linked above, a React equivalent example might look like:
Reducing duplication whenever needing to do destructuring assignments with type annotations. In particular it will solve the Svelte issue linked above, but applies anywhere you use destructuring and type annotations that you commonly see with function arguments.
What shortcomings exist with current approaches?
As discussed above already, it's unnecessarily verbose and duplicative, and prevents the colocation of types where you probably want to see them.
What workarounds are you using in the meantime?
Move the type declarations for destructured function params above/outside of the destructuring assignment, but this isn't actually addressing the problems solved by the new syntax.
The text was updated successfully, but these errors were encountered:
Wow that comment thread turned into a trainwreck at the end there. It seems to have completely devolved to “why you just shouldn’t use destructuring at all, and then you won’t need this feature” which is just… not productive at all
🔍 Search Terms
inline type annotations, type annotations, destructuring annotations, destructuring, property annotations, parameter destructuring,
✅ Viability Checklist
⭐ Suggestion
I've observed a lot of discussion around the verbosity/duplication involved when providing type annotations for destructured object properties (particularly re-ignited by this Svelte issue), which prompted me to wonder if this could be resolved with additional syntax being introduced to TypeScript.
The proposal suggests introducing new syntax for adding inline type annotations using angle brackets
<T>
within destructuring assignments that can be added immediately after the property keys, along with a newoptional
modifier keyword to indicate a property as optional.e.g. this
For completeness (though far less valuable), this could probably also support providing annotations in array destructuring assignments as well. e.g.
Advantages:
Drawbacks:
Other considerations:
optional
modifier keyword also work inside of object literal types.?
be used instead of the newoptional
modifier inside/outside of the brackets (e.g.url?<string>
orurl<string?>
). Would probably be more familiar to the way optional properties are defined now, but it might get confusing whether?
is part of the property name in the first example, or part of conditional logic in the second.📃 Motivating Example
Drawing inspiration from the Svelte
$props
issue linked above, a React equivalent example might look like:💻 Use Cases
Reducing duplication whenever needing to do destructuring assignments with type annotations. In particular it will solve the Svelte issue linked above, but applies anywhere you use destructuring and type annotations that you commonly see with function arguments.
As discussed above already, it's unnecessarily verbose and duplicative, and prevents the colocation of types where you probably want to see them.
Move the type declarations for destructured function params above/outside of the destructuring assignment, but this isn't actually addressing the problems solved by the new syntax.
The text was updated successfully, but these errors were encountered: