I have multiple enums
const enum E1 {
CLOSED = 'CLOSED',
OPEN = 'OPEN',
IN_PROGRESS = 'IN_PROGRESS',
}
const enum E2 {
OPEN = 'OPEN',
CLOSED = 'CLOSED',
IN_PROGRESS = 'IN_PROGRESS',
}
const enum E3 {
OPEN = 'OPEN',
IN_PROGRESS = 'IN_PROGRESS',
}
I want to create a function that accepts an enum that one of the values should be ‘CLOSED’
const func = (value: 'CLOSED') => {...}
interface Data1 {status: E1}
interface Data2 {status: E2}
interface Data3 {status: E3}
const o1: Data1 = {status: E1.CLOSED}
const o2: Data2 = {status: E2.OPEN}
const o3: Data3 = {status: E3.OPEN}
func(o1.status) // this should be valid. status is type E1, it contains 'CLOSED'
func(o2.status) // this should be valid. status is type E2, it contains 'CLOSED'
func(o3.status) // this should be invalid. status type is E3, it does not contain 'CLOSED'
Not it throws an error for each of them:
Argument of type E... is not assignable to parameter of type "CLOSED"
I don’t want that function to know about each enum, as they are many. This is why I used the type value: 'CLOSED'
Do I need something like a common enum to make others extend it somehow, so I can use its type in the argument instead of ‘CLOSED’?
Typescript version: 4.9.4
1