首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用yarn工作区时,tsc无法在apollo-server中找到express的声明文件

使用yarn工作区时,tsc无法在apollo-server中找到express的声明文件
EN

Stack Overflow用户
提问于 2021-09-11 21:38:04
回答 2查看 278关注 0票数 0

我正在尝试在其中一个包中使用yarn工作区(版本3.0.2)、typescript(4.4.3)和apollo-server (3.3.0)构建一个monorepo。运行yarn workspace graphql tsc --build时,我得到以下错误:

代码语言:javascript
复制
Could not find a declaration file for module 'express'. '...../.yarn/__virtual__/express-virtual-1169aebee1/0/cache/express-npm-4.17.1-6815ee6bf9-d964e9e17a.zip/node_modules/express/index.js' implicitly has an 'any' type.
  If the 'express' package actually exposes this module, consider sending a pull request to amend 'https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/express'

2 import express from 'express';
                      ~~~~~~~~~

然后我尝试安装@types/express,但错误仍然存在。如何解决此问题?

我的项目结构如下:

代码语言:javascript
复制
├── package.json
├── packages
│   └── graphql
│       ├── index.ts
│       ├── package.json
│       └── tsconfig.json
└── yarn.lock

我尝试编译的index.ts文件:

代码语言:javascript
复制
import { gql } from 'apollo-server';

// A schema is a collection of type definitions (hence "typeDefs")
// that together define the "shape" of queries that are executed against
// your data.
const typeDefs = gql`
  # Comments in GraphQL strings (such as this one) start with the hash (#) symbol.

  # This "Book" type defines the queryable fields for every book in our data source.
  type Book {
    title: String
    author: String
  }

  # The "Query" type is special: it lists all of the available queries that
  # clients can execute, along with the return type for each. In this
  # case, the "books" query returns an array of zero or more Books (defined above).
  type Query {
    books: [Book]
  }
`;

我的tsconfig.json

代码语言:javascript
复制
{
  "compilerOptions": {
    "target": "ES2020",
    "module": "commonjs",
    "outDir": "./dist",
    "strict": true,
    "incremental": true,
    "noUnusedLocals": true,
    "esModuleInterop": true,
    "noUnusedParameters": true,
    "noFallthroughCasesInSwitch": true,
    "baseUrl": "./src",
    "skipLibCheck": false
  },
  "include": ["**/*.ts"],
  "compileOnSave": true
}

我的根package.json

代码语言:javascript
复制
{
  "name": "test-monorepo",
  "packageManager": "yarn@3.0.2",
  "private": true,
  "workspaces": [
    "packages/*"
  ]
}

我的graphql包的package.json

代码语言:javascript
复制
{
  "name": "graphql",
  "packageManager": "yarn@3.0.2",
  "dependencies": {
    "@types/express": "^4.17.13",
    "@types/node": "^16.9.1",
    "apollo-server": "^3.3.0",
    "graphql": "^15.5.3",
    "typescript": "^4.4.3"
  }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-09-15 07:40:54

apollo-server不包括作为peerDependency@types/express。解决方案是告诉yarn包apollo-server需要.yarnrc.yaml中的@types/express

代码语言:javascript
复制
packageExtensions:
  apollo-server@*:
    dependencies:
      "@types/express": "*"
票数 0
EN

Stack Overflow用户

发布于 2021-09-14 09:25:40

tsconfig.json文件中,将skipLibCheck更改为true。这将跳过声明文件的类型检查。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69146897

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档