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

当前回答

参数'post'在Angularcli中隐含地有一个'any'类型 也许在项目创建时,你已经在应用中启用了Angular的严格模式? Max建议禁用严格模式 如果您已经启用了严格模式,请通过在tsconfig中设置strict属性来禁用它。Json为false

其他回答

尝试声明用户的类型。 如

let user:Object = {sample object}

遵循这个方法。 作为

let var:type = val

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

我在Angular的函数参数中发现了这个问题。

在我的代码给出错误之前

参数'event'隐含有一个'any'类型

这是代码

changeInpValue(event)
{
    this.inp = event.target.value;
}

这里是修改后的参数写:any,错误就解决了

changeInpValue(event : any)
{
    this.inp = event.target.value;
}

对我来说很好。

最小误差再现

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

如果你得到一个错误,因为参数“element”隐式具有“any”类型。 在vueJs

错误为:

 exportColumns.forEach(element=> {
      if (element.command !== undefined) {
        let d = element.command.findIndex(x => x.name === "destroy");

你可以通过如下定义这些变量来修复它。

纠正代码:

exportColumns.forEach((element: any) => {
      if (element.command !== undefined) {
        let d = element.command.findIndex((x: any) => x.name === "destroy");