Automatic "full" discriminated union

Hello, I have this code:

const ActorInputSchema = type({
    runMode: '"actor"',
    actorName: 'string',
    actorInput: 'unknown',
    'datasetId?': 'never',
    'exactData?': 'never',
})

const DatasetInputSchema = type({
    runMode: '"dataset"',
    'actorName?': 'never',
    'actorInput?': 'never',
    datasetId: 'string',
    'exactData?': 'never',
}) 

const ExactInputSchema = type({
    runMode: '"exact"',
    'actorName?': 'never',
    'actorInput?': 'never',
    'datasetId?': 'never',
    exactData: 'string'
})

const InputSchema = ActorInputSchema.or(DatasetInputSchema).or(ExactInputSchema)

type Input = typeof InputSchema.infer; 

const input = InputSchema(/* some data */);

if (input instanceof type.errors) {
    console.error(input.message); process.exit();
}

const {
    runMode,
    // Property actorName does not exist on type...
    actorName,
    actorInput,
    datasetId,
    exactData,
} = input as Input;

But in this case, I have to add field?: 'never' to make the compiler happy when destructuring the validated input, else it says that some field doesn't exist on it.

Is there a way to make this work, or is my approach wrong from the start? Thank you! :D
Was this page helpful?