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