我是 TypeScript 的新手,我不知道在调用库函数/方法时使用什么类型。例如,我在 Node.js 项目中使用 headless chrome 模块。
import puppeteer = require("puppeteer");
async function launchBrowser () {
const browser = await puppeteer.launch();
return browser;
}
// In this case I do not know the return type of the launch method. What should I do?
async function launchBrowser (): Promise<any> {
const browser: any = await puppeteer.launch();
return browser;
}
Run Code Online (Sandbox Code Playgroud)
我应该使用 any 还是不打字?
我正在开发一个普通的 Typescript 应用程序,并成功地通过导入我自己创建的模块进行了测试,这些模块是项目的一部分。现在我试图从“node_modules”文件夹中包含一个 3rd 方模块,但编译器无法找到该模块。
import * as pnp from '@pnp/pnpjs';
Run Code Online (Sandbox Code Playgroud)
以下是我对 tsconfig 的设置:
{
"compileOnSave": true,
"compilerOptions": {
"types": [],
"sourceMap": true,
"module": "amd",
"target": "es5",
"noEmitOnError": true,
"outDir": "./dist"
},
"exclude": [
"node_modules",
"obj",
"bin"
]
}
Run Code Online (Sandbox Code Playgroud)
下面是我的 package.json:
{
"version": "1.0.0",
"name": "OSD.SharePointHostedApp",
"private": true,
"devDependencies": {
"@types/chai": "^4.1.7",
"@types/sharepoint": "^2013.1.6"
},
"dependencies": {
"jquery": "^3.3.1",
"@pnp/pnpjs": "^1.2.5",
"chai": "^4.2.0"
}
}
Run Code Online (Sandbox Code Playgroud)
打字稿版本:1.0.3.0。(我尝试更新到最新,但它仍然显示 1.0.30.0,也许是因为这个??)
我在这里缺少什么?我已经检查并验证了 node_modules 中是否存在第 3 方模块。
更新
版本有问题。即使我安装了最新版本的 typescript 3.1.6,它也没有被选中,而是显示了旧版本 1.0.3.0。不知何故,Typescript 1.0 作为 Microsoft …
尽管遵循此处的建议 - Angular2 代码中的 TypeScript 错误:找不到名称 'module',我仍然收到此错误。我正在使用 Angular 5 和 npm 6.9.0。我正在尝试在这里完成本教程 - https://medium.freecodecamp.org/learn-how-to-create-your-first-angular-app-in-20-min-146201d9b5a7。
我在 ./src/app/app.component.ts 文件的顶部有这个
import { Component,trigger,animate,style,transition,keyframes } from '@angular/core';
import { FormsModule } from '@angular/forms';
@NgModule({
imports: [
BrowserModule,
FormsModule //<----------make sure you have added this.
],
})
Run Code Online (Sandbox Code Playgroud)
但是当我运行我的应用程序时出现错误
localhost:todo-app davea$ ng serve
** NG Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ **
Date: 2019-05-17T17:06:29.377Z
Hash: e9b5158cd1cb6d5685c7
Time: 2561ms
chunk {inline} inline.bundle.js (inline) 3.85 kB [entry] [rendered]
chunk {main} …Run Code Online (Sandbox Code Playgroud) 当我尝试使用“npm cache verify”时,出现以下错误
0 info it worked if it ends with ok
1 verbose cli [ '/home/noderuntime/output/bin/node',
1 verbose cli '/home/noderuntime/output/bin/npm',
1 verbose cli 'cache',
1 verbose cli 'verify' ]
2 info using npm@5.5.1
3 info using node@v8.9.1
4 verbose npm-session c361f37acc40114f
5 verbose stack Error: stream.push() after EOF
5 verbose stack at readableAddChunk (/home/noderuntime/output/lib/node_modules/npm/node_modules/readable-stream/lib/_stream_readable.js:264:30)
5 verbose stack at Class.Readable.push (/home/noderuntime/output/lib/node_modules/npm/node_modules/readable-stream/lib/_stream_readable.js:238:10)
5 verbose stack at Array.from.map.entry (/home/noderuntime/output/lib/node_modules/npm/node_modules/cacache/lib/entry-index.js:120:29)
5 verbose stack at Array.map (<anonymous>)
5 verbose stack at getKeyToEntry.then.reduced (/home/noderuntime/output/lib/node_modules/npm/node_modules/cacache/lib/entry-index.js:119:47)
5 …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 npm 安装 react-contenteditable。但是 eslint 的对等依赖项不允许我使用以下错误进行安装。我正在使用 Ubuntu 18.04 LTS 操作系统。
这是我用来安装 react-contenteditable 的代码。
npm i react-contenteditable
npm WARN @typescript-eslint/eslint-plugin@1.11.0 requires a peer of eslint@^5.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN @typescript-eslint/experimental-utils@1.11.0 requires a peer of typescript@* but none is installed. You must install peer dependencies yourself.
npm WARN @typescript-eslint/parser@1.11.0 requires a peer of eslint@^5.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN @typescript-eslint/typescript-estree@1.6.0 requires a peer of typescript@* but none …Run Code Online (Sandbox Code Playgroud) 我注意到一些 TypeScript 节点模块(例如loopback-next/packages)使用节点模块发布它们的源文件。这是否有特殊原因,还是只是不必要地增加了模块的大小?
我正在使用 React (16.12.0) 和 PhpStorm (2019.3.1)。
我导入的包是 react-router-dom (5.1.2)
我将编写以下导入:
import { NavLink, Switch } from "react-router-dom";
Run Code Online (Sandbox Code Playgroud)
这两个包都由 webpack/babel 正确导入,并且当我同时使用 Switch 和 NavLink 时,页面会正确呈现。
然而,纯粹从 IDE 的角度来看,我收到了关于 Switch 的警告: Cannot resolve symbol 'Switch'
这很奇怪,因为它显然就在那里,我登记入住/node_modules/react-router-dom并Switch.js在那里。
奇怪的是在之前版本的 PhpStorm 上(在几次更新和插件导入和其他更改之前),PhpStorm 准确地找到了 Switch 导入。
我最近没有更新 react-router-dom 并且正在使用其最新的稳定版本。
关于为什么可能缺少 Switch 的任何想法?
编辑: 我意识到这可能是因为 PhpStorm 难以导入 commonJS 模块。
我尝试将 Javascript 编译方法从 React JSX 更改为 ECMA 6。那没有用。
我还尝试为 react-router-dom 导入一些构建库,这有助于Route奇怪地识别导入但不是Switch.
我还尝试使 PhpStorm 缓存无效并重新启动应用程序,但这也不起作用。
编辑 2: 根据答案,当我的光标在其中时,我尝试使用 option+enter(Windows 上的 alt+enter) …
jetbrains-ide phpstorm webstorm node-modules react-router-dom
我想安装 1.8.x 版本的包,并且能够稍后在 >=1.8.0 <1.9.0 范围内自动更新此依赖项。
我试图运行这个命令:
npm install example-package@~1.8 --save
Run Code Online (Sandbox Code Playgroud)
不幸的是,它将此记录添加到我的package.json:
"example-package" : "^1.8.0"
Run Code Online (Sandbox Code Playgroud)
但我想要的是这个:
"example-package" : "~1.8.0"
Run Code Online (Sandbox Code Playgroud)
如何在npm install不手动编辑package.json文件的情况下使用 来做到这一点?
我使用的版本2.x的react-navigation,现在我迁移到版本5.x。我已根据文档安装了所有模块,但在运行应用程序时,出现此错误。
这似乎更像是打字稿编译错误,但我不确定它为什么会出现,因为我只是运行npm install命令并且没有触及任何node_module文件。
error: SyntaxError: E:\PROJECTS\rnzone\BoxApp\node_modules\@react-navigation\stack\src\index.tsx: Unexpected token (51:12)
49 | * Types
50 | */
> 51 | export type {
| ^
52 | StackNavigationOptions,
53 | StackNavigationProp,
54 | StackHeaderProps,
Run Code Online (Sandbox Code Playgroud)
你有什么建议?
node.js node-modules typescript react-native react-navigation
我npm install在package.json我公司项目的目录上。但是,它不断给我各种错误 Unexpected token in JSON at position...。
我已经做了npm cache clean --force,npm cache verify等所建议当我运行`NPM install`,它返回一个`ERR!代码完整性`(npm 5.3.0)
但是,问题仍然存在。我没有配置代理,因为我的机器不在任何公司代理上。
反正我附上npm日志内容和package.json文件内容供大家参考。请帮助告诉我出了什么问题以及如何解决这个问题。
npm 日志:
122 silly pacote range manifest for array-includes@^3.1.1 fetched in 64ms
123 http fetch GET 304 https://registry.npmjs.org/typescript 459ms (from cache)
124 silly pacote range manifest for popper.js@^1.14.1 fetched in 109ms
125 warn deprecated popper.js@1.16.1: You can find the new Popper v2 at @popperjs/core, this package is dedicated to the legacy v1 …Run Code Online (Sandbox Code Playgroud) node-modules ×10
node.js ×6
typescript ×5
npm ×4
npm-install ×3
angular ×1
javascript ×1
json ×1
loopback4 ×1
loopbackjs ×1
phpstorm ×1
react-native ×1
reactjs ×1
webstorm ×1