Typescript在Fn'上添加键入;s参数,带解构+;休息 问题

Typescript在Fn'上添加键入;s参数,带解构+;休息 问题,typescript,typescript-typings,Typescript,Typescript Typings,您好,尝试学习使用typescript打字,但在尝试添加使用typescript打字时遇到了一些问题: 我不知道如何将其转换为强类型的Typescript const omit = (prop: P, { [prop]: _, ...rest}) => rest; 因此,问题是如何在第二个参数中为已分解的对象添加类型 我试过的 我认为它应该是这样的,但不起作用: const omit = <P = string, R>(prop: P, { [prop]: _, ...

您好,尝试学习使用typescript打字,但在尝试添加使用typescript打字时遇到了一些问题:

我不知道如何将其转换为强类型的Typescript

const omit = (prop: P, { [prop]: _, ...rest}) => rest;

  • 因此,问题是如何在第二个参数中为已分解的对象添加类型
我试过的 我认为它应该是这样的,但不起作用:

const omit = <P = string, R>(prop: P, { [prop]: _, ...rest } : {[prop: string], rest: R }): R => rest;

const omit = <P = string, O, R = Omit<O,P>>(prop: P, { [prop]: _, ...rest } : {[prop: string]: O[P], rest: R }): R => rest;

const omit = <P = string, R>(prop: P, { [prop]: _, ...rest } : {[prop]: P, rest: R }): R => rest;

const-omit=(prop:P,{[prop]:{,{[rest}:{[prop:string],rest:R}):R=>rest;
常量omit=(prop:P,{[prop]:{,…[rest}:{[prop:string]:O[P],rest:R}):R=>rest;
const-omit=(prop:P,{[prop]:{,..rest}:{[prop]:P,rest:R}):R=>rest;

这实际上非常简单易懂。在使用泛型方面,您走的是正确的道路,但是您试图表达第二个参数的类型,即它将如何被分解以计算函数的结果,这不是这里的方法

相反,通过声明无约束泛型,允许第二个参数采用任何类型,并将第一个参数的类型指定为约束为泛型实例化时使用的任何类型(即第二个参数的类型)的任何属性键

const omit = <O, P extends keyof O>(prop: P, { [prop]: _, ...rest }: O): Omit<O, P> => rest;
const o = { a : 1, b: 2 };
const r = omit('a', o);
const-omit=(prop:P,{[prop]:,…rest}:O):omit=>rest;
常数o={a:1,b:2};
常数r=省略('a',o);

请注意,函数的返回类型将正确推断为
ommit
,无需指定

const omit = <O, P extends keyof O>(prop: P, { [prop]: _, ...rest }: O) => rest;
const-omit=(prop:P,{[prop]:,…rest}:O)=>rest;

您有严重的语法错误,很难理解您要实现的目标。我建议您1)提供一个简单的JS示例,说明您试图在TypeScript中建模的内容,2)学习TypeScript教程。不确定上面的示例有什么严重的地方,但我已经将文档刷新了。很抱歉把你弄糊涂了,我已经说过下面我展示的代码块是不起作用的。谢谢你的回答,所以我把事情复杂化了,我认为typescript不能表示这个复杂的表单。这应该是最简单的方法!