[英]Is there any way in typescript to define optional field with type?
I need something like that:我需要这样的东西:
interface A {
a: SomeUtilityType<number>;
}
to be equivalent to:相当于:
interface A {
a?: number;
}
I know that I can use Optional
from 'utility-types':我知道我可以使用“实用程序类型”中的Optional
:
import { Optional } from 'utility-types';
type B = Optional<A, 'a'>
but I need exactly usage like this:但我需要这样的用法:
interface A {
a: SomeUtilityType<number>;
}
If you are looking how to define optional field in interface, you can use question mark the same as in you first example如果您正在寻找如何在界面中定义可选字段,您可以使用与第一个示例相同的问号
interface A {
a?: SomeUtilityType<number>;
}
?:
operator can be used with primitives, and any other types too ?:
运算符可以与原语一起使用,也可以与任何其他类型一起使用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.