简体   繁体   English

object 类型推断中的文字值

[英]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?有没有办法在这里使用类型推断但强制值是文字?

Use as const : as const

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM