簡體   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