简体   繁体   English

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

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

I'm relatively new to Typescript and don't have much familiarity with setting up the requisite environment;我对 Typescript 比较陌生,对设置必要的环境不太熟悉; any help would be appreciated.任何帮助,将不胜感激。 I'm running to an issue with the below code:我遇到了以下代码的问题:

models/Player.ts模型/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
  }
}

utils.ts工具.ts

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

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

This code gives me the error: Unsafe return of an 'any' typed value.此代码给我错误: Unsafe return of an 'any' typed value. in the new Player() portion.new Player()部分。

However, if the code is all in one file, ie no imports, there is no error.但是,如果代码全部在一个文件中,即没有导入,则没有错误。 I figure there's something in the typescript or eslint configuration somewhere that's incorrect but I've no ideas.我认为 typescript 或 eslint 配置中的某处不正确,但我不知道。

Edit: my tsconfig.json编辑:我的 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/**/*"]
}

I have this problem also:我也有这个问题:

tsconfig.json : tsconfig.json

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

utils/format.ts : 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);
}

and I am getting a error ( Unsafe call of an any typed value.eslint@typescript-eslint/no-unsafe-call ) when I import this file here:当我在此处导入此文件时出现错误( 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.

相关问题 Typescript 不安全地返回“任何”类型的值 - Typescript Unsafe return of an `any` typed value Typescript:ESLint:任何类型值的不安全返回 @typescript-eslint/no-unsafe-return - Typescript : ESLint : Unsafe return of an any typed value @typescript-eslint/no-unsafe-return UIComponent.getRouterFor 显示 TypeScript 错误:“任何”类型值的不安全返回 - UIComponent.getRouterFor shows TypeScript error: Unsafe return of an 'any' typed value Typescript Jest 函数的 ESLint 错误(“any”类型值的不安全调用) - Typescript ESLint errors with Jest functions (Unsafe call of an `any` typed value) ESLint:“any”的不安全分配和“any”类型值的不安全调用 - ESLint: Unsafe assignment of an `any` and Unsafe call of an `any` typed value 不安全的成员访问<method>在 `any` 值上。 `this` 在编译器项目的 TypeScript 中被键入为 `any`。 如何为 TypeScript 构建 mixin?</method> - Unsafe member access <method> on an `any` value. `this` is typed as `any` in TypeScript for compiler project. How to architect mixins for TypeScript? @typescript-eslint/no-unsafe-assignment: `any` 值的不安全赋值 - @typescript-eslint/no-unsafe-assignment: Unsafe assignment of an `any` value @typescript-eslint/no-unsafe-assignment:任何值的不安全赋值 - @typescript-eslint/no-unsafe-assignment: Unsafe assignment of an any value 任何值 ESLint 错误的 Typescript 不安全分配 - Typescript Unsafe Assignment of An Any Value ESLint Error 为什么 TypeScript 允许 Function 返回不安全值 - Why is TypeScript Allowing Function to Return Unsafe Value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM