Consider a scenario where we have an interface
For example:
interface Person {
name: string;
age: number ;
}
We aim to develop a generic function that can take the following parameters
const res = Result.combineValues<Person>(
{ age: 18 },
{ name: 'John Doe' }
);
At this stage, our current implementation looks like this
class Result<T> {
readonly value: T;
private constructor(value: T) {
this.value = value;
}
public static combineValues<T>(...results: { [key in keyof T]?: Result<T[key]> | T[key] }[]): Result<T> {
let value: T;
// ... compute value
return new Result<T>(value);
}
}
The issue we are facing is that the function allows undefined values
const res = Result.combineValues<Person>(
{ age: undefined }, // This should throw a compile error as age must be a number or Result<number>
{ name: 'John Doe' }
);
Moreover, it does not ensure that all properties are defined
const res = Result.combineValues<Person>(
{ age: 18 }
// A compile error should be raised here because `{name: 'Some Name'}` is missing from the argument list
);