【发布时间】:2021-09-08 03:31:09
【问题描述】:
先阅读这个问题可能会有所帮助:How to copy the structure of one generic type to another generic in TypeScript?
给定以下输入类型:
interface InputType {
age: number;
surname: string;
}
我想要一个可以为上述输入生成以下输出类型的函数:
type OutputValue<T> = (val: T) => void;
interface OutputType {
age: OutputValue<number>;
surname: OutputValue<string>;
}
这个函数的签名是:
type OutputType<T> = {
[k in keyof T]: OutputValue<T[k]>
}
type TransformationFunction<Input, Output extends OutputType<Input>> = (input: Input) => Output;
上面的类型签名将确保输出是类型安全的。这意味着我可以使用智能感知来正确检索output.surname 函数(例如)。
棘手的部分是以类型安全的方式返回正确的数据结构。
这是我的尝试:
const transformString = (stringValue: string) => {
return (val: string) => {
// some code processing val
}
}
const transformationFunction = function<Input, Output extends GenericMap<Input>>(input: Input): Output {
const keys = Object.keys(input) as Array<keyof Input>;
return keys.reduce((output: Output, key: keyof Input) => {
const inputValue = input[key];
if (/*typeguard*/ isString(inputValue)) {
return transformString(inputValue); // typescript compiler complains
}
else if ( /*typeguard*/ isNumber(inputValue)) {
return transformNumber(inputValue); // similar function to above + typescript complains
}
else {
throw new Error("No transform");
}
}, {} as Output)
}
如何将自定义值应用到输出对象?
【问题讨论】:
-
有点不清楚您的实际设置是什么。或许提供playground 链接?
标签: javascript typescript typescript-generics