Answer the question
In order to leave comments, you need to log in
How to write a function that returns a class depending on the parameter?
There is a set of classes.
class TimeoutTextValidator {
public static type = 'timeout-text' as const
public schema = {timeout: 1}
}
class ImageValidator {
public static type = 'image' as const
public schema = {src: ''}
}
type
. export function getValidatorByType(type) {
switch (type) {
case 'timeout-text':
return TimeoutTextValidator
case 'image':
return ImageValidator
default:
return assertUnreachable(type)
}
}
function assertUnreachable(_: never): never {
throw new Error("Didn't expect to get here");
}
Answer the question
In order to leave comments, you need to log in
I would do this:
https://www.typescriptlang.org/play?#code/PTAEiQQR...
This is what you need: https://www.typescriptlang.org/play?#code/MYGwhgzh... ?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question