'作为const'结合类型?
我想将两个类型的类型结合在一起,并使用它“作为const”将字面类型作为类型:
type MyType = {name: string};
const x:MyType = {
name: 'test' // Autocompleted, typesafe. But Type is {name: string}, not
// what I want, {name: 'test'}
}
const x = { name: 'test' } as const; // Gives correct type, but no type check...
如何做到这一点?
I want to combine both having a type for a constant, and using it "as const" to get the literal types as the type:
type MyType = {name: string};
const x:MyType = {
name: 'test' // Autocompleted, typesafe. But Type is {name: string}, not
// what I want, {name: 'test'}
}
const x = { name: 'test' } as const; // Gives correct type, but no type check...
How to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编辑Sep 2022:
使用Typescript 4.9现在我们可以使用 noreferrer“>满足 操作员:
如果出于某种原因,您无法使用
满足
,那么复制相同的行为很容易:以前的解决方案:
这是一种实现目标的方法您想要:
Edit Sep 2022:
With Typescript 4.9 we can now use
satisfies
operator:And if for some reason, you can't use
satisfies
, it's fairly easy to replicate the same behavior:Previous solution:
Here is a way to achieve what you want:
无需键入检查
{name:“ test”}作为const
,因为Typescript使用结构性等于{name:“ test”}作为const
是MyType的结构相同,因此相同,因此相同。可以使用这些辅助类型观察到这种行为。
任何接受mytype的东西都可以进行类型的测试。
编辑:如果您想强制测试是类型的mytype进行类型检查测试,则无法通过保持字符串字面的字体来做到这一点。
这意味着,如果您想在mytype上同时使用字面和字符串类型,则需要做类似的事情(更改mytype)。注意使用它似乎是详细的,但可以说比“ const”
^ ^ sning和字面类型的样板要少。
There is no need to type-check
{name: "test"} as const
because Typescript uses structural equality meaning aslong as{name: "test"} as const
is the same structure of MyType they will be equal and therefore the same.This behaviour can be observed using these helper types.
Anything that takes a MyType can take a typeof Test.
EDIT: If you want to force test to be of type MyType to type-check test there you cannot do this by keeping the string literal because anything asserted to be MyType will lose the literal type and fall back to string this behaviour can be observed here.
Meaning if you want to have both Literal and string types on MyType you will need to instead do something like this (changing MyType). Note using this seems verbose but is arguably less boilerplate than "as const"
^ Both string and literal types allowed.
我认为没有任何原始支持。
但是,您可以通过定义另一个使用类型断言来进行打字。
注意:我使用“ typeof”来避免引用X值的引用,以确保如果没有其他参考,可以删除X的值。
I don’t think there is any primitive support.
But you can do typechecking by defining another const with type assertion.
Note:I use 'typeof' for avoiding reference of x's value which ensures that x's value can be deleted if there is not any other reference.