admin管理员组文章数量:1403187
enum KINDS {
STATIC = 1,
FIELD,
ARG,
VAR
}
enum ALL_KINDS {
STATIC = 1,
FIELD,
ARG,
VAR,
NONE
}
How can I reuse the first enum inside the second one?
enum KINDS {
STATIC = 1,
FIELD,
ARG,
VAR
}
enum ALL_KINDS {
STATIC = 1,
FIELD,
ARG,
VAR,
NONE
}
How can I reuse the first enum inside the second one?
Share Improve this question asked Sep 29, 2019 at 19:07 Henok TesfayeHenok Tesfaye 9,62015 gold badges51 silver badges92 bronze badges 2-
By "re-use" you mean you want
ALL_KINDS
to include the entries, with the same values, fromKINDS
? – Dave Newton Commented Sep 29, 2019 at 19:13 - Yup @DaveNewton. – Henok Tesfaye Commented Sep 29, 2019 at 20:07
1 Answer
Reset to default 4AFAIK, extending enums is under consideration, in the meantime you can use const objects instead:
const KINDS = <const>{
STATIC: 1,
FIELD: 2,
ARG: 3,
VAR: 4
};
const ALL_KINDS = <const>{ ...KINDS, NONE: 5 };
There are also other workarounds in the above thread.
If you want this type to be checked, note that from the type perspective, a numeric enum
is equivalent to number
:
enum KINDS {
STATIC,
FIELD,
ARG,
VAR
}
declare function func(name: string, type: string, kind: KINDS): any;
func('foo', 'bar', KINDS.ARG); // piles
func('foo', 'bar', 99); // piles too (?)
If you use an object as suggested above, you can also enforce strict type checking by creating a type for all possible values of that object:
const KINDS = <const>{
STATIC: 1,
FIELD: 2,
ARG: 3,
VAR: 4
};
type KIND_VALUE = typeof KINDS[keyof typeof KINDS]
declare function define(name: string, type: string, kind: KIND_VALUE): any;
define('foo', 'bar', KINDS.ARG); // piles
define('foo', 'bar', 99); // doesn't pile
This is a bit more verbose, but then you have your type actually checked.
本文标签: javascriptUsing an enum inside enum in typescriptStack Overflow
版权声明:本文标题:javascript - Using an enum inside enum in typescript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744384869a2603687.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论