[英]TypeScript, const/enums type
I have the following const/enum:我有以下常量/枚举:
export const LENGTH_LIMIT = {
MAX_INPUT_CUSTOMER_FORM: '500',
MAX_INPUT_EMPLOYEE_FORM: '500'
};
I would like to give it a type.我想给它一个类型。 But can i give
LENGTH_LIMIT
a type besides any
?但是我可以给
LENGTH_LIMIT
一个除了any
的类型吗? Or is any
the only thing which would work here?或者是
any
只有这将在这里工作的事情吗?
1) That's an object, not an enum 1) 那是一个对象,而不是一个枚举
2) Why do you want to give constants a type? 2)为什么要给常量一个类型? Typescript already infers them for you.
Typescript 已经为你推断出来了。 It infers this:
它推断出:
interface LenghtLimit {
MAX_INPUT_CUSTOMER_FORM: string,
MAX_INPUT_EMPLOYEE_FORM: string
}
You could use TypeScript enums for this.您可以为此使用TypeScript 枚举。 Then, the type would be the enum itself:
然后,类型将是枚举本身:
export enum LENGTH_LIMIT {
MAX_INPUT_CUSTOMER_FORM = '500',
MAX_INPUT_EMPLOYEE_FORM = '500',
}
And to use it:并使用它:
interface CustomerForm {
InputLimit: LENGTH_LIMIT
}
you can create an interface for example:您可以创建一个接口,例如:
interface LengthLimit {
[name: string || any]: string
}
then:然后:
export const LENGTH_LIMIT : LengthLimit = {
MAX_INPUT_CUSTOMER_FORM: '500',
MAX_INPUT_EMPLOYEE_FORM: '500'
};
If you want declare as enum:如果你想声明为枚举:
export enum LENGTH_LIMIT {
MAX_INPUT_CUSTOMER_FORM = '500',
MAX_INPUT_EMPLOYEE_FORM = '500',
}
If you want to use a variable of type LENGTH_LIMIT in HTML template you must assign it:如果要在 HTML 模板中使用 LENGTH_LIMIT 类型的变量,则必须为其分配:
LengthLimitOptions: any = LENGTH_LIMIT;
And in HTML use as:在 HTML 中用作:
<span>{{LengthLimitOptions.MAX_INPUT_EMPLOYEE_FORM}}</span>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.