[英]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.