I've just stumbled across this post from 2018 that explains how to do it with string based enums (see the original comment).
The key seems to be to declare a type AND a const with the same name.
Here's an annotated / fruity version:
enum someEnum {
Apple = 'Apple',
Banana = 'Banana'
}
enum extendedEnum {
Pear = 'Pear',
Grape = 'Grape'
}
// The key seems to be to declare a type AND
// a const with the same name
type AllFruits = someEnum | extendedEnum;
const AllFruits = {...someEnum, ...extendedEnum};
let f: AllFruits = AllFruits.Grape;
The original poster (who, by all rights, seems to have been a contributor to TypeScript and knows what they're talking about) mentions that, using this method, you can't use something like AllFruits.Grape as a 'type literal', which means you can't do this:
// This error will appear:
// 'AllFruits' only refers to a type, but is being used as a namespace here.
interface FruitBowl {
fruit: AllFruits.Grape
}
but this can be fixed with (something quite ugly) like:
interface FruitBowl {
fruit: typeof AllFruits.Grape
}
I guess this is one of the 'type workarounds' that others have mentioned.
(All credit to https://github.com/alangpierce)
enum someEnum { c = 'string', b = someEnum.b }typescript will merge them together… but I would not recommend doing this as it will pollute the original enumenumi.e. have to use one of thetypeworkarounds, and according to the TS Program Manager this is unlikely to change. See github.com/microsoft/TypeScript/issues/…