相关疑难解决方法(0)

无法重新声明块作用域变量(打字稿)

我正在构建一个节点应用程序,并且.js中的每个文件内部用于执行此操作以在各种包中要求.

let co = require("co");
Run Code Online (Sandbox Code Playgroud)

但是得到

在此输入图像描述

所以使用打字稿似乎在整个项目中只能有一个这样的声明/要求?我对此感到困惑,因为我认为let它的范围是当前文件.

我刚刚有一个正在运行的项目但是在重构之后我现在正在处理这些错误.

谁能解释一下?

require typescript

66
推荐指数
9
解决办法
6万
查看次数

Typescript指南提供"重复功能实现"警告

我开始使用TypeScript,现在我在5分钟的指南中关注TypeScript.当我将鼠标悬停在greeter函数名称上时,我在Visual Studio Code中收到一个奇怪的警告,如下图所示.警报是:

[ts]重复的功能实现.

function greeter(person:Person):string(+1 overload)

重复的功能实现警告.

但是我的单个文件中没有这个独特功能的其他实现!当我运行tsc greeter.ts所有工作正常并生成js文件.

完整greeter.ts档案:

interface Person {
    firstName: string;
    lastName: string;
}

function greeter(person: Person) {
    return "Hello, " + person.firstName + " " + person.lastName;
}

var user = { firstName: "Jane", lastName: "User" };

console.log(greeter(user));
Run Code Online (Sandbox Code Playgroud)

我为什么收到此警报?怎么解决?我看了一下这个问题,但我相信它没有关系.

javascript typescript

30
推荐指数
6
解决办法
8555
查看次数

使用Typescript 2.8反应Native - 无法重新声明块范围变量'console'

使用Typescript创建了一个react-native入门项目,可以在2周前运行.现在,当我尝试运行项目时,它会在打字稿编译过程中出错.

skipLibraryCheck: true在tsconfig.json中使用可修复错误.但是为什么它会从〜/ .nvm和./node_modules目录中抛出错误?

错误(针对node_modules/.bin/tsc更新):

>  node_modules/.bin/tsc
node_modules/@types/react-native/index.d.ts(8742,11): error TS2451: Cannot redeclare block-scoped variable 'console'.
node_modules/@types/react-native/index.d.ts(8750,18): error TS2717: Subsequent property declarations must have the same type.  Property 'geolocation' must be of type 'Geolocation', but here has type 'GeolocationStatic'.
node_modules/@types/react-native/index.d.ts(8753,11): error TS2451: Cannot redeclare block-scoped variable 'navigator'.
node_modules/redux-thunk/index.d.ts(4,47): error TS2314: Generic type 'Dispatch<A, S>' requires 2 type argument(s).
node_modules/redux-thunk/index.d.ts(8,20): error TS2428: All declarations of 'Dispatch' must have identical type parameters.
node_modules/redux/index.d.ts(115,18): error TS2428: All declarations of 'Dispatch' must have identical …
Run Code Online (Sandbox Code Playgroud)

typescript react-native

11
推荐指数
1
解决办法
3700
查看次数

将Node.js项目从纯ES6迁移到TypeScript

是否已开始将Node.js项目从普通ES6迁移到TypeScript.

我做了什么:

npm install -g typescript
npm install @types/node --save-dev
Run Code Online (Sandbox Code Playgroud)

设置tsconfig.json:

{
     "compilerOptions": {
         "emitDecoratorMetadata": true,
         "experimentalDecorators": true,
         "moduleResolution": "node",
         "module": "commonjs",
         "target": "es6",
         "sourceMap": true,
         "outDir": "dist",
         "allowJs": true,
         "forceConsistentCasingInFileNames": true
     },
     "exclude": [
         "node_modules",
         "dist",
         "docs"
     ]
}
Run Code Online (Sandbox Code Playgroud)

将所有文件扩展名更改.js.ts(除了node_modules):

find . -not \( -path node_modules -prune \) -iname "*.js" -exec bash -c 'mv "$1" "${1%.js}".ts' - '{}' \;
Run Code Online (Sandbox Code Playgroud)

tsc现在运行会导致大量错误,例如:

server.ts:13:7 - error TS2451: Cannot redeclare block-scoped variable 'async'.

13 …
Run Code Online (Sandbox Code Playgroud)

node.js typescript typescript3.0

7
推荐指数
2
解决办法
843
查看次数

重复函数实现

我有一个文件,app.ts我下scripts的文件夹中,被复制到wwwroot/scripts一个gulp任务。在后gulp任务运行,我现在也有一个wwwroot/scripts/app.ts文件,其中唯一功能是红色下划线标记为重复。这是正常的,还是我gulp在下面的任务声明不正确?

var paths = {
    scripts: ["scripts/**/*.js", "scripts/**/*.ts", "scripts/**/*.map"]
};

gulp.task("default", function() {
    gulp.src(paths.scripts).pipe(gulp.dest("wwwroot/scripts"));
});
Run Code Online (Sandbox Code Playgroud)

我看到原始app.ts文件,从根scripts文件夹也被内置到*.js*.js.map文件。这可能与“误报”重复功能有关吗?

javascript visual-studio typescript gulp visual-studio-2017

7
推荐指数
1
解决办法
617
查看次数

如何在多个 TypeScript 文件中导入相同的 npm 模块?

问题:

\n

每当我在多个 TypeScript 文件中导入相同的 npm 模块时,error TS2451: Cannot redeclare block-scoped variable \'os\'一旦运行 TypeScript 编译器,我就会收到错误“” tsc。我的项目设置如下所示。

\n

我正在使用 NodeJS v18.15.0。

\n
\n

设置:

\n

目录结构

\n
Project_Root\n\xe2\x94\x9c\xe2\x94\x80 main.ts\n\xe2\x94\x9c\xe2\x94\x80 worker.ts\n\xe2\x94\x9c\xe2\x94\x80 package.json\n\xe2\x94\x94\xe2\x94\x80 tsconfig.json\n
Run Code Online (Sandbox Code Playgroud)\n

主要.ts

\n
const worker = require(\'./worker\');\nconst os = require(\'os\');\n\nconsole.log(\'name = \' + worker.getName() );\nconsole.log(\'memory = \' + os.freemem() );\n
Run Code Online (Sandbox Code Playgroud)\n

工人.ts

\n
const os = require(\'os\');\n\nmodule.exports =\n    class worker {\n        static getName() : string { return "worker-" + os.freemem(); }\n    }\n
Run Code Online (Sandbox Code Playgroud)\n

包.json

\n
Project_Root\n\xe2\x94\x9c\xe2\x94\x80 …
Run Code Online (Sandbox Code Playgroud)

javascript commonjs node.js npm typescript

5
推荐指数
1
解决办法
183
查看次数