在测试我的UserRouter时,我使用了一个json文件

data.json

[
  {
    "id": 1,
    "name": "Luke Cage",
    "aliases": ["Carl Lucas", "Power Man", "Mr. Bulletproof", "Hero for Hire"],
    "occupation": "bartender",
    "gender": "male",
    "height": {
      "ft": 6,
      "in": 3
    },
    "hair": "bald",
    "eyes": "brown",
    "powers": [
      "strength",
      "durability",
      "healing"
    ]
  },
  {
  ...
  }
]

构建我的应用程序,我得到以下TS错误

ERROR in ...../UserRouter.ts
(30,27): error TS7006: Parameter 'user' implicitly has an 'any' type.

UserRouter.ts

import {Router, Request, Response, NextFunction} from 'express';
const Users = require('../data');

export class UserRouter {
  router: Router;

  constructor() {
  ...
  }

  /**
   * GET one User by id
   */
  public getOne(req: Request, res: Response, _next: NextFunction) {
    let query = parseInt(req.params.id);
 /*[30]->*/let user = Users.find(user => user.id === query);
    if (user) {
      res.status(200)
        .send({
          message: 'Success',
          status: res.status,
          user
        });
    }
    else {
      res.status(404)
        .send({
          message: 'No User found with the given id.',
          status: res.status
        });
    }
  }


}

const userRouter = new UserRouter().router;
export default userRouter;

当前回答

最小误差再现

export const users = require('../data'); // presumes @types/node are installed
const foundUser = users.find(user => user.id === 42); 
// error: Parameter 'user' implicitly has an 'any' type.ts(7006)

推荐解决方案:——resolveJsonModule

The simplest way for your case is to use

--解析Json模块

compiler option:
import users from "./data.json" // `import` instead of `require`
const foundUser = users.find(user => user.id === 42); // user is strongly typed, no `any`!

除了静态JSON导入,还有其他一些替代方案。

选项1:显式用户类型(简单,不检查)

type User = { id: number; name: string /* and others */ }
const foundUser = users.find((user: User) => user.id === 42)

方案2:类型防护(中间)

类型警卫

are a good middleground between simplicity and strong types:
function isUserArray(maybeUserArr: any): maybeUserArr is Array<User> {
  return Array.isArray(maybeUserArr) && maybeUserArr.every(isUser)
}

function isUser(user: any): user is User {
  return "id" in user && "name" in user
}

if (isUserArray(users)) {
  const foundUser = users.find((user) => user.id === 42)
}
You can even switch to

断言函数

(TS 3.7+) to get rid of if and throw an error instead.
function assertIsUserArray(maybeUserArr: any): asserts maybeUserArr is Array<User> {
  if(!isUserArray(maybeUserArr)) throw Error("wrong json type")
}

assertIsUserArray(users)
const foundUser = users.find((user) => user.id === 42) // works

选项3:运行时类型的系统库(复杂)

对于更复杂的情况,可以集成像io-ts或ts-runtime这样的运行时类型检查库。


不推荐的解决方案

noImplicitAny:错

undermines many useful checks of the type system:
function add(s1, s2) { // s1,s2 implicitely get `any` type
  return s1 * s2 // `any` type allows string multiplication and all sorts of types :(
}
add("foo", 42)

最好为User提供显式的User类型。这将避免将任何类型传播到内层。相反,输入和验证保留在外层API层的JSON处理代码中。

其他回答

在tsconfig中添加以下规则。json文件: "strict": false /*不允许所有严格类型检查选项。* /,

转到tsconfig。Json和注释行//strict:true这对我有用

我的情况:


export default function ({user, path}) {
    return (
        //...
    )
}

我改成了

export default function ({user, path} : {
    user: string
    path: string
}) {
    return (
        //...
    )
}

在tsconfig的compilerOptions部分进行这些更改。Json文件,这为我工作

"noImplicitAny": false

无需设置

"strict":false

有些电脑编译很慢,请等1 - 2分钟

最小误差再现

export const users = require('../data'); // presumes @types/node are installed
const foundUser = users.find(user => user.id === 42); 
// error: Parameter 'user' implicitly has an 'any' type.ts(7006)

推荐解决方案:——resolveJsonModule

The simplest way for your case is to use

--解析Json模块

compiler option:
import users from "./data.json" // `import` instead of `require`
const foundUser = users.find(user => user.id === 42); // user is strongly typed, no `any`!

除了静态JSON导入,还有其他一些替代方案。

选项1:显式用户类型(简单,不检查)

type User = { id: number; name: string /* and others */ }
const foundUser = users.find((user: User) => user.id === 42)

方案2:类型防护(中间)

类型警卫

are a good middleground between simplicity and strong types:
function isUserArray(maybeUserArr: any): maybeUserArr is Array<User> {
  return Array.isArray(maybeUserArr) && maybeUserArr.every(isUser)
}

function isUser(user: any): user is User {
  return "id" in user && "name" in user
}

if (isUserArray(users)) {
  const foundUser = users.find((user) => user.id === 42)
}
You can even switch to

断言函数

(TS 3.7+) to get rid of if and throw an error instead.
function assertIsUserArray(maybeUserArr: any): asserts maybeUserArr is Array<User> {
  if(!isUserArray(maybeUserArr)) throw Error("wrong json type")
}

assertIsUserArray(users)
const foundUser = users.find((user) => user.id === 42) // works

选项3:运行时类型的系统库(复杂)

对于更复杂的情况,可以集成像io-ts或ts-runtime这样的运行时类型检查库。


不推荐的解决方案

noImplicitAny:错

undermines many useful checks of the type system:
function add(s1, s2) { // s1,s2 implicitely get `any` type
  return s1 * s2 // `any` type allows string multiplication and all sorts of types :(
}
add("foo", 42)

最好为User提供显式的User类型。这将避免将任何类型传播到内层。相反,输入和验证保留在外层API层的JSON处理代码中。