我希望构建一个功能,它可以接收任何对象并返回带有几个附加属性的对象。类似于:
```javascript function addProperties(obj) { // 在此添加新属性 return obj; } ```
对于上述代码,TypeScript编译器返回错误,表示接口只能添加类或接口。我该如何在TypeScript中表达这个意思?
```javascript function addProperties(obj) { // 在此添加新属性 return obj; } ```
//this code doesn't work
function addProperties<T>(object: T): IPropertiesToAdd<T> {/*implmentions code*/};
interface IPropertiesToAdd<T> extend T{
on(): void;
off(): void;
}
//usage example
var str = new String('Hello')
addProperties(str)
str.charAt(3)
str.on()
对于上述代码,TypeScript编译器返回错误,表示接口只能添加类或接口。我该如何在TypeScript中表达这个意思?