202
votes

I'm creating a TypeScript definition file for a 3rd party js library. One of the methods allows for an options object, and one of the properties of the options object accepts a string from the list: "collapse", "expand", "end-expand", and "none".

I have an interface for the options object:

interface IOptions {
  indent_size?: number;
  indent_char?: string;
  brace_style?: // "collapse" | "expand" | "end-expand" | "none"
}

Can the interface enforce this, so if you include an IOptions object with the brace_style property, it will only allow a string that is in the acceptable list?

6
Please revisit the answers to this question - Andreas
Kinda along the lines of the comment from @Andreas , why do you prefer RyanQ's answer over Denis Khay's? Denis' seems more broadly applicable imo. - ruffin

6 Answers

306
votes

This was released in version 1.8 as "string literal types"

What's New in Typescript - String Literal Types

Example from the page:

interface AnimationOptions {
  deltaX: number;
  deltaY: number;
  easing: "ease-in" | "ease-out" | "ease-in-out";
}
197
votes

Try this

export type ReadingTypes = 'some'|'variants'|'of'|'strings';

export interface IReadings {
   param:ReadingTypes
}

Edit: Many thanks for upvotes, but, as time passed and me evolved as a developer :), now in most of the cases I wouldn't recommend this approach anymore. Yes it still valid but the point is the construction above is very similar to enum structure so why not use enum instead (advantages below):

export enum ReadingTypes {
    Some = 'some',
    Variants = 'variants',
    Of = 'of',
    Strings = 'strings',
}
export interface IReadings {
   param: ReadingTypes
}

Advantages: (Yes, might be it is more like IMHO, I understand, but, nonetheless)

  1. It is more readable when you see it in the code, for example
if(item.reading === 'some') {
...
}
// vs 
if(item.reading === ReadingTypes.Some) {
...
}

In first case when you read the code you could not catch, from the first glance, that .reading field can only contain few certain params, and not like, any string value.

  1. When you write the code you will have better assistance of your editor if you use enums - it is enough to remember name of enum and write it and it will show you all variants of enum. Yeah, with the first type ('some' | 'variants' ... ) it can do so too, but it does it less.. um.. eagerly
13
votes

TS offers a typing to specific string values, which are called String literal types.

Here is an example of how to use them:

type style =  "collapse" | "expand" | "end-expand" | "none";

interface IOptions {
  indent_size?: number;
  indent_char?: string;
  brace_style1?:  "collapse" | "expand" | "end-expand" | "none";
  brace_style2?:  style;
}

// Ok
let obj1: IOptions = {brace_style1: 'collapse'};

// Compile time error:
// Type '"collapsessss"' is not assignable to type '"collapse" | "expand" | "end-expand" | "none" | undefined'.
let obj2: IOptions = {brace_style1: 'collapsessss'};
11
votes

Maybe not exactly what you wanted, but Enums seem like a perfect solution for you.

enum BraceStyle {Collapse, Expand, EndExpand, None}

interface IOptions {
  indent_size?: number;
  indent_char?: string;
  brace_style?: BraceStyle
}

Enums are, however, number-based. It means that during runtime a real value for e.g. BraceStyle.Collapse will be 0 in this case. But you can use them with other, even non-typescript scripts, since they compile to objects. This is how BraceStyle will look after compile&run:

{
    0: "Collapse",
    1: "Expand",
    2: "EndExpand",
    3: "None",
    Collapse: 0,
    Expand: 1,
    EndExpand: 2,
    None: 3
}

If you want strings instead, you can use a class with static members, as described here

8
votes

In TypeScript 2.4 onward you can use String Enums

I favour this approach because it avoids the need to have the same hard coded string in more than one place.

Its possible to make an enum where the values are strings

export enum VISIBILITY {
  PUBLISH = "publish",
  DRAFT = "draft"
}

This enum can then be used as a type on an interface or class

export interface UserOptions  {
  visibility:  VISIBILITY 
}
1
votes
function keysOf<T>(obj: T, key: keyof T) { return obj[key]; }
interface SomeInterface {
   a: string;
}
const instance: SomeInterface = { a: 'some value'};
let value = keysOf<SomeInterface>(instance, 'b'); // invalid
value =  keysOf<SomeInterface>(instance, 'a'); // valid