admin管理员组文章数量:1390482
Context
In a JavaScript file, let require
be a variable (whether top-level or local, in a function, etc.), that does not work like the CommonJS one:
function () {
...
/**
* @param {string} s
* @returns {number}
*/
function require(name) { ... }
const someNumber = require('someName');
...
}
Playground demo
Issue
If I use it (as above), TypeScript assumes someNumber
is an import and raises a type import error:
Cannot find module 'someName' or its corresponding type declarations.
From my understanding this is because TypeScript assumes any variable named require
is the CommonJS import object.
Is there some way to make a variable named require
work as any other variable?
Additional details
I could not find a way to disable this behavior in the tsconfig.json
file.
I can specify the output type:
/** @type {number} */
const someNumber = require('someName');
but ideally I would not want to change the code on use site of require
, only where it is declared.
本文标签: commonjsMake quotrequirequot work like any function with TypeScript type inferenceStack Overflow
版权声明:本文标题:commonjs - Make "require" work like any function with TypeScript type inference - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744589393a2614393.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论