[英]ESLint: Unsafe assignment of an `any` and Unsafe call of an `any` typed value
Consider the following test code:考虑以下测试代码:
import { isHtmlLinkDescriptor } from '@remix-run/react/links'
import invariant from 'tiny-invariant'
import { links } from '~/root'
it('should return a rel=stylesheet', () => {
const response = links()
invariant(isHtmlLinkDescriptor(response[0]))
expect(response[0].rel).toBe('stylesheet')
})
and the implementation:和实施:
import { LinksFunction } from 'remix'
import tailwindProdUrl from '~/styles/tailwind.css'
export const links: LinksFunction = () => {
const href =
process.env.NODE_ENV !== 'production' ? './tailwind.css' : tailwindProdUrl
return [{ rel: 'stylesheet', href }]
}
Where LinksFunction
is defined here and here . LinksFunction
在此处和此处定义的位置。
Why is ESLint complaining about "Unsafe assignment of an any
value."为什么 ESLint 抱怨“
any
值的不安全分配”。 and "Unsafe call of an any
typed value."和“
any
类型值的不安全调用”。 in the line below?在下面的行中?
const response = links()
You have a wrong import in the second snippet.您在第二个片段中有错误的导入。 It should be:
它应该是:
import { LinksFunction } from '@remix-run/server-runtime';
or also, if you are working with TypeScript 3.8 or later:或者,如果您使用的是 TypeScript 3.8 或更高版本:
import type { LinksFunction } from '@remix-run/server-runtime';
The fact that LinksFunction
is being only used as a type in the code you are showing means that the TypeScript compiler could still succeed in compiling your modules under certain circumstances, despite the missing type definitions. LinksFunction
仅用作您显示的代码中的类型这一事实意味着 TypeScript 编译器在某些情况下仍然可以成功编译您的模块,尽管缺少类型定义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.