TypeScript
TypeScript copied to clipboard
type destructuring
Search Terms
type destructuring
Suggestion
Allow destructuring syntax when assigning type
Use Cases
Dynamically assign a type as the type of an other type's property.
Examples
Where AutocompleteService
is a class, PlacesServiceStatus
is an enum, and AutocompletePrediction
is an interface.
What I'm doing currently:
const { AutocompleteService, PlacesServiceStatus } = google.maps.places;
type AutocompletePrediction = google.maps.places.AutocompletePrediction;
What I'd like to be able to do:
const { AutocompleteService, PlacesServiceStatus } = google.maps.places;
type { AutocompletePrediction } = google.maps.places;
Checklist
My suggestion meets these guidelines:
- [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
- [x] This wouldn't change the runtime behavior of existing JavaScript code
- [x] This could be implemented without emitting different JS based on the types of the expressions
- [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- [x] This feature would agree with the rest of TypeScript's Design Goals.
This would actually be very tricky because x.t
and x["t"]
mean different things in a type context, so it's unclear which a destructuring should be doing since neither is implied by the syntax.
I would also like to see this added. I just jumped on this repo today to request the same when I saw someone else recently post another dupe of this one.
@RyanCavanaugh What if we just restrict this new destructuring syntax to always mean x.t
and never x["t"]
so there's no ambiguity?
I often export types from a file like this:
export * as FieldTypes from './form-field-types.js`;
It would be handy to then be able to pull out those types I want from that bundled type namespace like this:
import { FieldTypes } from './some-index.js';
type { Input, Number, Range } = FieldTypes;