Derive Type from Parameter of function attach to object (possibly recursive idk)

Hey Guys, so to explain what I'm doing, I'm basically defining a data structure to plug into a function of mine. The data structure is a key value and a class object that is defined on the fly. Like this
const AccountValidation: ValidationShape = {
username: Validation.new('').length(5),
password: Validation.new('').length(5)
}
const AccountValidation: ValidationShape = {
username: Validation.new('').length(5),
password: Validation.new('').length(5)
}
The Validation.new() function is used to define the default value for each field and instantiate an object to build my validation strategy.
type ValidationShape = {
[key: string ]: Validation<unknown>
}
type ValidationShape = {
[key: string ]: Validation<unknown>
}
This is the ValidationShape type and here is the Validation object class
export class Validation<T> {
default: T;
ValidationQueue: ValidationMethod[] = [];

constructor(val: T) {
this.default = val;
}

length(min: number, max?: number) {
this.ValidationQueue.push({
method: 'length',
min,
max

})
return this;
}

getDefaultValue(): T {
return this.default;
}

getValidationQueue(): ValidationMethod[] {
return this.ValidationQueue;
}

static new<T>(val: T) {
return new Validation(val);
}
export class Validation<T> {
default: T;
ValidationQueue: ValidationMethod[] = [];

constructor(val: T) {
this.default = val;
}

length(min: number, max?: number) {
this.ValidationQueue.push({
method: 'length',
min,
max

})
return this;
}

getDefaultValue(): T {
return this.default;
}

getValidationQueue(): ValidationMethod[] {
return this.ValidationQueue;
}

static new<T>(val: T) {
return new Validation(val);
}
So this is the object attached to each invidual key fpr my ValidationShape object. Now, I would like to take this shape, iterate over it and get the default value and its type and then take those values to create a new shape that is the key: ReturnType<GetDefaultValue>
type Reshape = {
[Property in keyof ValidationShape]: ReturnType<Property['getDefaultValue']>
}


const T: Reshape = {
username: "usernme",
password: "passwrd",
}
type Reshape = {
[Property in keyof ValidationShape]: ReturnType<Property['getDefaultValue']>
}


const T: Reshape = {
username: "usernme",
password: "passwrd",
}
Notice the reshape version is now just strings, since each new call was called with an empty string.
1 Reply
benten
benten2y ago
Sorry, what's the issue here?
Want results from more Discord servers?
Add your server