简体   繁体   English

在 TypeScript 中将对象属性复制到另一个,导致错误 TS2339

[英]Copy a object properties to another in TypeScript, cause error TS2339

I use a extend() function to extend a object:我使用一个extend()函数来扩展一个对象:

function extend(obj: Object, extension: Object) {
    for (var key in obj) {
        try {
            extension[key] = obj[key];
        } catch (e) {
            console.error(e);
        }
    }
}

And there is a class Observer() hava a method Update() and a object check which is HTMLInputElement type.并且有一个类Observer()有一个方法Update()和一个HTMLInputElement类型的对象check

class Observer {
    Update(value) { }
}
var check:HTMLInputElement = document.createElement("input");

I use extend() function to extend checkbox ,so it would have the method Update() .我使用extend()函数来扩展checkbox ,所以它会有方法Update()

extend(new Observer(), check);
check.Update = function(value) {
    this.checked = value;
}

And then cause error TS2339: Property 'Update' does not exist on type 'HTMLInputElement'然后导致错误 TS2339: Property 'Update' does not exist on type 'HTMLInputElement' 在此处输入图片说明

How to Fixed this error?如何修复此错误? Change the extend() function?更改extend()函数?

Might be a good scenario for an intersection type ?对于交叉点类型来说可能是一个好场景?

function extend<T, U>(obj: T, extension: U) {
    Object.keys(obj).forEach((key) => {
        extension[key] = obj[key];
    });

    return extension as T & U;
}

var check: HTMLInputElement;
var extended = extend(new Observer(), check);

// example HTMLInputElement property use
extended.attributes;
// no compile error here either:
extended.Update = function(value) {
    this.checked = value;
};

Is the following code snippet, what you are looking for?以下代码片段是您要查找的内容吗?

class Observer extends HTMLInputElement{
    Update: any;
}

var check:Observer = <Observer>document.createElement("input");

check.Update = function(value) {
    this.checked = value;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误 TS2339:“文件上传”类型上不存在属性“_input” - error TS2339: Property '_input' does not exist on type 'FileUpload Typescript TS2339 属性在类型上不存在 - 类声明 - Typescript TS2339 Property doesn't exist on type - Class decleration 导出的打字稿类的属性和方法不存在(TS 2339) - Properties and methods on exported typescript classes don't exist (TS 2339) 如何在 TypeScript 中处理此方法链接? 错误 ts(2339) - How to approach this method chaining in TypeScript? Error ts(2339) TS2339:类型“FindUserProps”上不存在属性“电子邮件” - TS2339: Property 'email' does not exist on type 'FindUserProps' TS2339:联合类型上不存在属性-属性字符串| 未定义 - TS2339: Property does not exist on union type - property string | undefined 自定义 Object 返回错误:类型“void”上不存在属性“forEach”.ts(2339) - Custom Object Returning Error: Property 'forEach' does not exist on type 'void'.ts(2339) TypeScript 复制对象的属性创建一个新的 object - TypeScript copy object's properties to create a new object 如何在 ts 中使用“@devexpress/dx-react-scheduler”? 它会导致 Typescript 错误 - How can i use '@devexpress/dx-react-scheduler' in ts? It cause Typescript error Typescript:TS2739:缺少属性 - Typescript: TS2739: properties are missing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM