Typescript: Shorthand for types/interfaces with properties of same type

Created on 28 Feb 2019  ·  1Comment  ·  Source: microsoft/TypeScript

Search Terms

Interface shorthand, sugar syntax, interfaces of same type, types of same type

Suggestion

Can a shorthand be added to create interfaces/types that have all keys of same type.

Use Cases


Reduce the no of lines & also help in readability.

Examples


export interface AllStrings { prop1: string; prop2: string; }

to something like

export interface AllStrings<type: string> { prop1,prop2; }

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.)
  • [- ] This feature would agree with the rest of TypeScript's Design Goals.
Question

Most helpful comment

What about:

type AllStrings = Record<"prop1" | "prop2", string>

>All comments

What about:

type AllStrings = Record<"prop1" | "prop2", string>
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Roam-Cooper picture Roam-Cooper  ·  3Comments

CyrusNajmabadi picture CyrusNajmabadi  ·  3Comments

MartynasZilinskas picture MartynasZilinskas  ·  3Comments

wmaurer picture wmaurer  ·  3Comments

jbondc picture jbondc  ·  3Comments