I'm struggling with making my code declarative enough. I want to infer a type inside an interface scope and then use that same type as an argument for a method within the interface. Here is a simple example:
interface Prop {
x: infer U,
// ^^^^^^^ store type coming from 'x'
validate: (x: U) => U
// ^ use type
}
interface IState {
[key: string]: Prop
}
Here's a use case scenario:
const state:IState = {
asString: {
x: '',
validate: value => value + ' is string',
// ^^^^^ string
},
asBoolean: {
x: true,
validate: value => !value;
// ^^^^^ boolean
}
}
Do you think this approach is feasible?