繁体   English   中英

Typescript 导入 class:不安全返回“任何”类型的值

[英]Typescript imported class: Unsafe return of an `any` typed value

我对 Typescript 比较陌生,对设置必要的环境不太熟悉; 任何帮助,将不胜感激。 我遇到了以下代码的问题:

模型/Player.ts

export interface PlayerI {
  health: number
  resources: number
  turnsPlayed: number
  drawCard?: (card: Card) => void
}

export default class Player implements PlayerI {
  health: number
  resources: number
  turnsPlayed: number

  constructor() {
    this.health = 10000
    this.resources = 3000
    this.turnsPlayed = 0
  }
}

工具.ts

import Player, {PlayerI} from '@models/Player'

export function createPlayer(): PlayerI {
  return new Player()
}

此代码给我错误: Unsafe return of an 'any' typed value. new Player()部分。

但是,如果代码全部在一个文件中,即没有导入,则没有错误。 我认为 typescript 或 eslint 配置中的某处不正确,但我不知道。

编辑:我的 tsconfig.json

{
 "compilerOptions": {
   "target": "es2016",                           
   "lib": ["es6"],                               
   "module": "commonjs",                         
   "rootDir": "src",                             
   "moduleResolution": "node",                   
   "baseUrl": "./src",                           
   "paths": {
     "@models/*": ["./models/*"],
     "@utils/*": ["./utils/*"],
   },                                            
   "resolveJsonModule": true,                    
   "allowJs": false,                             
   "outDir": "build",                            
   "esModuleInterop": true,                      
   "forceConsistentCasingInFileNames": true,     
   "strict": true,                               
   "noImplicitAny": true,                        
   "skipLibCheck": true                          
 },
 "exclude": ["jest.config.ts"],
 "include": [
   "src/**/*"]
}

我也有这个问题:

tsconfig.json

{
  "extends": "../../tsconfig.base.json",
  "compilerOptions": {
    "rootDir": "./",
    "baseUrl": "./lib"
  },
  "include": ["lib/**/*"]
}

utils/format.ts

export default function format(text: string, ...args: string[]): string {
  return args.reduce((acc, arg, iArg) => acc.replace(new RegExp(`:\\{${iArg}\\}`, 'g'), arg), text);
}

当我在此处导入此文件时出现错误( Unsafe call of an any typed value.eslint@typescript-eslint/no-unsafe-call ):

import format from 'utils/format';

console.log('db-schema-builder say hello', format(':{0} :{1} !!!', 'Hello', 'World'));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM