Typescript - 重复标识符错误 - const x: ({ y: string, z: string }) => string;
Typescript - Duplicate identifier error - const x: ({ y: string, z: string }) => string;
为什么下面的代码在参数中标记string
s会报错Duplicate identifier 'string'.(2300)
?
const append: ({ first: string, second: string }) => string = ({ first, second }) => first + second;
console.log(append({ first: "a", second: "b" }));
不过,它可以正确编译和运行。
复制
- 在此处粘贴代码:https://www.typescriptlang.org/play
- 您会发现错误突出显示。
- 如果您单击 "Run",它会正确记录
ab
。
你没有正确定义类型,你想要
const append: (input: { first: string, second: string }) => string = ({ first, second }) => first + second;
在更明确的条目中,这与:
interface Input {
first: string;
second: string;
}
interface Append {
(input: Input): string;
}
const append: Append = ({first, second}) => {
return first + second;
}
为什么下面的代码在参数中标记string
s会报错Duplicate identifier 'string'.(2300)
?
const append: ({ first: string, second: string }) => string = ({ first, second }) => first + second;
console.log(append({ first: "a", second: "b" }));
不过,它可以正确编译和运行。
复制
- 在此处粘贴代码:https://www.typescriptlang.org/play
- 您会发现错误突出显示。
- 如果您单击 "Run",它会正确记录
ab
。
你没有正确定义类型,你想要
const append: (input: { first: string, second: string }) => string = ({ first, second }) => first + second;
在更明确的条目中,这与:
interface Input {
first: string;
second: string;
}
interface Append {
(input: Input): string;
}
const append: Append = ({first, second}) => {
return first + second;
}