tavoyne
tavoyne

Reputation: 6539

Literal values in object type inference

This is quite verbose:

interface Point {
  x: 1;
  y: 2;
}

const point: Point = {
  x: 1,
  y: 2,
};

// The inferred type would have been
// { x: number; y: number; }

Is there any way to use type inference here but to force values to be literals?

Upvotes: 1

Views: 208

Answers (1)

Matthieu Riegler
Matthieu Riegler

Reputation: 54639

Use as const:

const point = {
  x: '1',
  y: '2',
} as const; // { x: '1'; y: '2'; }

Upvotes: 3

Related Questions