在打字稿中找不到名称“省略”

时间:2019-06-04 08:38:32

标签: typescript

背景

我正在index.d.ts上创建我的自定义src/@types/index.d.ts文件。 我需要像下面那样合并我的类型。

// src/@types/index.d.ts
declare namespace Admin {
  interface InitialStateFromDB {
    teamSettings: {
      teamPasswords: TeamPassword[],
      teamCount: number
    },
    adminPasswords: string,
    postInfos: PostInfo[] | undefined
  }
  interface InitialState extends Omit<InitialStateFromDB, 'adminPasswords'> {
    adminPasswords: AdminPassword
  }
}

问题

我收到了VSCode Intellisense的错误:Cannot find name 'Omit'.ts(2304)。但是编译效果很好。

因此,我将省略类型设为type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>(复制到here中)

编译之后,我又遇到了另一个错误:'Omit' was also declared here.

在这种情况下我该怎么办?只是忽略IntelliSense的错误?

4 个答案:

答案 0 :(得分:5)

Omit只是最近added in TS 3.5.1,因此您可能正在工作空间中使用尚不支持TS的旧版本。

通过遵循these instructions,确保VS Code工作区使用的是与您正在编译的TypeScript版本相同的TypeScript。只需打开一个TS文件,在VS Code中运行Select TypeScript Version命令,然后选择使用您的工作区版本的TypeScript(应该为3.5.1)

答案 1 :(得分:2)

使用打字稿版本 3.5.x

如果您使用的是 Angular 版本 6,请将其升级到 8.2.2

您可以在package.json

中查看以下软件包版本
"dependencies": {
    "@angular-devkit/build-angular": "^0.803.24",
    "@angular/animations": "^8.2.2",
    "@angular/cdk": "^8.2.2",
    "@angular/common": "^8.2.2",
    "@angular/compiler": "^8.2.2",
    "@angular/core": "^8.2.2",
    "@angular/forms": "^8.0.0",
    "@angular/http": "^8.0.0-beta.10",
    "@angular/material": "^8.2.2",
    "@angular/platform-browser": "^8.2.2",
    "@angular/platform-browser-dynamic": "^8.2.2",
    "@angular/router": "^6.0.0",
    "@material/dialog": "^4.0.0",
    "@ng-bootstrap/ng-bootstrap": "^4.0.0",
    "@ngx-translate/core": "^10.0.1",
    "@ngx-translate/http-loader": "^3.0.1",
    "@syncfusion/ej2-angular-charts": "^17.4.51",
    "@syncfusion/ej2-angular-diagrams": "^17.4.51",
    "@syncfusion/ej2-angular-grids": "^17.4.51",
    "@syncfusion/ej2-diagrams": "^17.4.51",
    "chart.js": "^2.7.2",
    "core-js": "^2.5.5",
    "file-saver": "^2.0.1",
    "font-awesome": "^4.7.0",
    "html2canvas": "^1.0.0-alpha.3",
    "jspdf": "^1.5.3",
    "lodash": "^4.17.15",
    "ng2-charts": "^1.6.0",
    "nyc": "^15.1.0",
    "rxjs": "^6.4.0",
    "stream": "0.0.2",
    "timers": "^0.1.1",
    "xml2js": "^0.4.19",
    "yarn": "^1.6.0",
    "zone.js": "^0.9.1"
},
"devDependencies": {
    "@angular/cli": "^8.2.2",
    "@angular/compiler-cli": "^8.2.2",
    "@angular/language-service": "^8.2.2",
    "@types/jasmine": "~2.8.6",
    "@types/jasminewd2": "~2.0.3",
    "@types/lodash": "4.14.116",
    "@types/node": "^9.6.1",
    "@types/xml2js": "^0.4.3",
    "codelyzer": "~4.2.1",
    "jasmine-core": "~3.1.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "^4.4.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^1.4.2",
    "karma-jasmine": "~1.1.1",
    "karma-jasmine-html-reporter": "^1.0.0",
    "protractor": "^6.0.0",
    "ts-node": "~5.0.1",
    "tslint": "~5.9.1",
    "typescript": "^3.5.3"
}

答案 2 :(得分:1)

在vscode中按ctrl + shift + p并键入TypeScript,然后选择“ TypeScript:选择TypeScript版本”

答案 3 :(得分:1)

尝试使用 "skipLibCheck": true 作为 tsconfig.json 文件中的 compilerOptions 之一