Answer the question
In order to leave comments, you need to log in
How to type a method?
Now
Challenge
public get<T>(prop: Constants): T {}
const VERTICAL = 'vertical';
const STEP = 'step';
this.model.get(VERTICAL) // VERTICAL значит должен возвращать только boolean
this.model.get(STEP) // STEP значит должен возвращать только number
// это не предлагать)
<boolean>this.model.get(VERTICAL)
<number>this.model.get(STEP)
public add({ value, prop }: ActionsModel): void { }
interface Action<T> {
prop: T;
}
interface ActionPayload<T, Y> extends Action<T> {
value: Y;
}
type ActionStep = ActionPayload<Constants.STEP, number>;
type ActionVertical = ActionPayload<Constants.VERTICAL, boolean>;
type ActionsModel = ActionStep | ActionVertical;
Answer the question
In order to leave comments, you need to log in
I would like to know what kind of Constants is, perhaps everything can be made simpler.
But conditionally - something like this:
enum Constants {
VERTICAL = 'vertical',
STEP = 'step'
}
interface ConstantsTypes {
[Constants.VERTICAL]: boolean,
[Constants.STEP]: number,
}
public get<T extends keyof ConstantsTypes>(prop: T): ConstantsTypes[T] {}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question