gpt4 book ai didi

TypeScript:引用 resig 的扩展

转载 作者:搜寻专家 更新时间:2023-10-30 21:31:43 24 4
gpt4 key购买 nike

我正在尝试引用使用 Resig 的“扩展”的现有代码,但我遇到了一堆错误

-------- 测试.ts --------

/// <reference path="myclass.js" />
var m = new MyClass (3);

-------- myclass.js --------

/// <reference path="class.js" />

var MyClass = Class.extend({

init: function (i)
{
this.i = i;
},
})

-------- 类.js --------

(copied from http://ejohn.org/blog/simple-javascript-inheritance/)

错误:

Supplied parameters do not match any signature of call target
The name 'Class' does not exist in the current scope
The property 'extend' does not exist on value of type '() => void'
The name 'Class' does not exist in the current scope

我意识到最终我想将基于扩展的代码重写为 TypeScript,但在那之前,我如何从新代码中引用它?

我想这引出了更深层次的问题 - 为什么它会提示现有 javascript 代码中的类型错误?

最佳答案

TypeScript 通常无法从外部 JavaScript 代码推断类型。

您需要声明您正在调用的“扩展”代码的形状,以便 TypeScript 知道该类型的形状是什么:

declare class Class {
static extend(body: any);
}

您可以将它直接放在您的源文件中(如果您只有一个文件项目),或者更恰本地说,放在您从源文件中引用的“.d.ts”文件中。

关于TypeScript:引用 resig 的扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12694678/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com