在测试我的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;

当前回答

我遇到了这个错误,并发现这是因为在tsconfig中“strict”参数被设置为true。json文件。设置为“false”(显然)。在我的例子中,我从cmd提示符中生成了tsconfig文件,只是错过了“strict”参数,该参数位于文件的更下方。

其他回答

最小误差再现

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处理代码中。

你正在使用——noImplicitAny, TypeScript不知道Users对象的类型。在这种情况下,您需要显式地定义用户类型。

更改这一行:

let user = Users.find(user => user.id === query);

:

let user = Users.find((user: any) => user.id === query); 
// use "any" or some other interface to type this argument

或者定义用户对象的类型:

//...
interface User {
    id: number;
    name: string;
    aliases: string[];
    occupation: string;
    gender: string;
    height: {ft: number; in: number;}
    hair: string;
    eyes: string;
    powers: string[]
}
//...
const Users = <User[]>require('../data');
//...

尝试声明用户的类型。 如

let user:Object = {sample object}

遵循这个方法。 作为

let var:type = val

在你的tsconfig中。在compilerOptions下设置参数"noImplicitAny": false来消除这个错误。

我的情况:


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

我改成了

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