TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

type destructuring

Open mina-skunk opened this issue 5 years ago • 2 comments

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.

mina-skunk avatar Nov 27 '19 23:11 mina-skunk

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.

RyanCavanaugh avatar Dec 02 '19 22:12 RyanCavanaugh

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;

brandonmcconnell avatar May 03 '23 02:05 brandonmcconnell