我在此文件中收到 ESlint 错误
酒吧.ts:
class Bar {
constructor(public foo: string) {}
hello(): string {
return this.foo;
}
}
export default Bar;
Run Code Online (Sandbox Code Playgroud)
我的 eslint 配置:
{
"env": {
"browser": true,
"commonjs": true,
"es2020": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"airbnb/base",
"eslint-config-prettier"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 11
},
"plugins": ["@typescript-eslint/eslint-plugin", "eslint-plugin-prettier"],
"rules": {
"prettier/prettier": "error",
"no-unused-vars": "warn",
"@typescript-eslint/no-var-requires": "warn",
"func-names": "off",
"no-underscore-dangle": "off",
"class-methods-use-this": "off"
},
"overrides": [
{
"files": ["*.ts"],
"excludedFiles": ["*.js"],
"rules": {
"@typescript/no-var-requires": "off"
}
} …Run Code Online (Sandbox Code Playgroud) 我有一个nodejs项目(用JS)。不幸的是,我必须利用很多节点全局变量。
一切工作正常(甚至很多人建议不要使用全局变量),除了:
全局变量没有智能感知。所以每次我想使用全局函数/对象时,我都需要查看它的代码并找出参数是什么,它返回什么等等。
假设我有一个全局变量,它是一个纯对象:
foo = {
bar: {
level2: {
level3: {
level4: "abc
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
处理它是相当烦人的,因为我在使用它时无法“看到”对象的结构,并且在编写代码时很容易出错。
我发布这个问题的原因是 ...npm 包
有很多用 vanilla JS 编写的包,其中大多数都使用强大的 d.ts 文件。安装该包后,您可以从项目中的任何位置使用它,并且 VS code 将为它们提供智能感知。如果您单击 VS 代码的工具提示(不知道它怎么称呼...类型定义工具提示?),您将导航到包的 d.ts 文件(不是命令的实际实现)。
所以我的问题是如何在我的项目中做同样的事情。我不会将其发布为 npm,我只想在项目中的某个位置有一个 d.ts 文件,这样我就可以使用我的全局文件,而无需在每次需要回忆它的作用时查看其实现。
我正在使用 tsconfig 来检查我的 js 文件。我的自定义类型定义在custom_types\custom.d.ts
tsconfig.json
{
"compilerOptions": {
"target": "es2020",
"module": "commonjs",
"allowJs": true,
"checkJs": true,
"outDir": "./dist",
"strict": true,
"typeRoots": ["./custom_types/", "node_modules/@types/"],
"types": ["node"],
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src/**/*"]
}
Run Code Online (Sandbox Code Playgroud)
自定义类型/自定义.d.ts
declare global {
const foo: string;
namespace NodeJS {
interface Global {
foo: string;
}
}
}
export default global;
Run Code Online (Sandbox Code Playgroud)
src/app.js
let bar = foo; // Cannot find name 'foo'.ts(2304)
Run Code Online (Sandbox Code Playgroud)
但是,当我在 tsconfig 中执行此操作时:
{
...
},
"include": ["src/**/*", "custom_types/custom.d.ts"]
}
Run Code Online (Sandbox Code Playgroud)
一切都很完美。所以这让我认为我tsconfig …
具有多个参数的函数的控制台输出
我有这个类方法:
searchForProduct({productName, manufacturer, seller}, itemsPerPage = 20, onlyAvailable = true) {
console.log(Searching for...) // Here's what is my question about
//do stuff
}
Run Code Online (Sandbox Code Playgroud)
如何打印出我传递给方法的所有参数?
想要实现的是:
searchForProduct({productName: laptop});
// Output:
"Searching for productName: 'laptop'"
// or
searchForProduct({productName: "laptop", manufacturer: "Dell"});
// Output:
"Searching for productName: 'laptop', manufacturer: 'Dell'"
Run Code Online (Sandbox Code Playgroud)
等等...
另外(如果可以使用任何建议的方法)我不想打印出默认值itemsPerPage,onlyAvailable即使它会被传递给方法。
更新:
哇,我没想到答案中有这么多巧妙的方法。但是我应该承认我不允许更改此功能。基本上我只需要根据我的个人需求添加此输出,因为更改此方法将“破坏一切”。
我很抱歉你花时间建议传递对象而不是破坏的参数。无论如何我都会赞成你的答案
UPD2:
我尝试了一些建议,但仍然没有达到完美的结果:
searchForProduct({productName, manufacturer, seller}, itemsPerPage = 20, onlyAvailable = true) {
function buildString({firstArgument, secondArgument, thirdArgument}) {
return {
productName: (firstArgument !== undefined) …Run Code Online (Sandbox Code Playgroud)