我正在将一个旧库从js移植到typescript以便在Vue 3中使用,但是它得到了一个错误:* “new”表达式,其目标缺少构造签名,隐式具有“any”类型。ts(7009)* 我认为错误来自我的包中的以下代码,但我还没有找到解决方案,以下是代码:
function M3dia(this: any, opts: any ) {
this.clientOpts = opts {};
this.apiVersion = this.clientOpts.version 'v1';
this.baseURL = ${this.clientOpts.server}/${this.apiVersion};
this.token = this.clientOpts.token null;
this.files = null;
this.endpoints = this.clientOpts.endpoints {
upload: 'files/upload',
chunk: 'files/upload/chunk',
auth: 'auth',
youtubeDownload: 'youtubes/download',
facebookDownload: 'facebooks/download'
};
this.chunkPhase = 'start';
this.chunks = [];
this.chunkActive = [];
this.chunkStartOffset = 0;
this.chunkMaxActive = this.clientOpts.maxActiveChunk || 3;
this.chunkMaxRetries = 1;
this.fileSize = 0;
this.chunkFile = null;
this.chunkSize = null;
this.chunkSessionId = null;
}
M3dia.prototype = {
getToken: async function () {
var self = this;
try {
var assignUrl = ${self.baseURL}/${self.endpoints.auth};
var data = {
'username': self.clientOpts.user.username,
'password': self.clientOpts.user.password
};
const response = await self._postx(assignUrl, data);
if (response.data && response.data.access_token) {
self.token = response.data.access_token;
}
return self.token;
} catch (error) {
return error;
}
},
}
...
下面是我在Vue中使用的代码,它给出了上述错误警告:
import m3dia from 'm3dia'
const m3 = new m3dia(
{
server: 'myServer',
user: {
username: 'myUserNamw',
password: 'abc123'
},
version: 'v2',
endpoints: {
upload: 'files/upload',
chunk: 'files/upload/chunk',
auth: 'users/signin',
youtubeDownload: 'youtubes/download',
facebookDownload: 'facebooks/download'
}
});
我希望会有一个解决方案来帮助我解决这个问题...
1条答案
按热度按时间bq3bfh9z1#
在TypeScript中使用
new
调用构造函数时会出现此错误。这是常见问题。有两种解决方法:1.将构造函数重构为类。示例:
至
2.强制类型(黑客解决方案).示例:
在我看来,这应该只在你不能编辑类的情况下使用。它看起来和感觉上都很笨拙,不知道这种语法会支持多久。
在您的示例中,这将是:
(3.您可以使用
//@ts-ignore
)忽略TS相关错误