[英]Type 'string' is not assignable to type
const opts = {
provider: 'SG',
logLevel: 'info', // <--- here is what the error is referring to
providerBaseUrl:
...
错误:
test/contract/api.provider.spec.ts:81:47 - error TS2345: Argument of type '{ provider: string; logLevel: string; providerBaseUrl: string; stateHandlers: { 'Has list of stations': () => Promise<string>; 'Has list of terminals': () => Promise<string>; 'Has list of centerlines': () => Promise<...>; }; ... 5 more ...; providerVersion: string; }' is not assignable to parameter of type 'VerifierOptions'.
Types of property 'logLevel' are incompatible.
Type 'string' is not assignable to type '"error" | "trace" | "debug" | "info" | "warn" | "fatal" | undefined'.
81 const output = await new Verifier(opts).verifyProvider();
~~~~
Found 1 error.
为什么 TypeScript 会抱怨。 我显然正在为该属性设置一个字符串值。
正如问题下的评论所暗示的那样,这是因为string
与字符串文字。
有两种/三种方法可以解决这个问题:
// Type safe approach
const opts: VerifierOptions = {
provider: 'SG',
logLevel: 'info',
或者:
// Not type safe approach
const opts = {
provider: 'SG',
logLevel: 'info' as VerifierOptions['logLevel'],
如果您需要使用变量:
// Not type safe approach
const opts = {
provider: 'SG',
logLevel: someVar as VerifierOptions['logLevel'],
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.