Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 如何获得一个免费的';aString';值匹配一个literal';aString';类型?_Typescript - Fatal编程技术网

Typescript 如何获得一个免费的';aString';值匹配一个literal';aString';类型?

Typescript 如何获得一个免费的';aString';值匹配一个literal';aString';类型?,typescript,Typescript,在将项目转换为TypeScript时,我遇到了以下问题 当我将作为“http”删除时在此处强制转换: 我在这里得到一个错误: “http”字符串似乎未被识别为“http”类型?! 通过使用以下命令npminit@whook运行项目副本,您可以轻松地实时检查问题 错误: Argument of type '{ openapi: string; info: { version: string; title: any; description: any; }; servers: { url: st

在将项目转换为TypeScript时,我遇到了以下问题

当我将
作为“http”删除时
在此处强制转换:

我在这里得到一个错误:

“http”字符串似乎未被识别为“http”类型?! 通过使用以下命令
npminit@whook
运行项目副本,您可以轻松地实时检查问题

错误:

Argument of type '{ openapi: string; info: { version: string; title: any; description: any; }; servers: { url: string; }[]; components: { securitySchemes: { fakeAuth: { type: "apiKey"; description: string; name: string; in: string; }; bearerAuth: { ...; }; } | { ...; }; }; paths: any; }' is not assignable to parameter of type 'Document'.
  Types of property 'components' are incompatible.
    Type '{ securitySchemes: { fakeAuth: { type: "apiKey"; description: string; name: string; in: string; }; bearerAuth: { type: string; description: string; scheme: string; }; } | { bearerAuth: { type: string; description: string; scheme: string; }; }; }' is not assignable to type 'ComponentsObject'.
      Types of property 'securitySchemes' are incompatible.
        Type '{ fakeAuth: { type: "apiKey"; description: string; name: string; in: string; }; bearerAuth: { type: string; description: string; scheme: string; }; } | { bearerAuth: { type: string; description: string; scheme: string; }; }' is not assignable to type '{ [key: string]: ReferenceObject | HttpSecurityScheme | ApiKeySecurityScheme | OAuth2SecurityScheme | OpenIdSecurityScheme; }'.
          Type '{ fakeAuth: { type: "apiKey"; description: string; name: string; in: string; }; bearerAuth: { type: string; description: string; scheme: string; }; }' is not assignable to type '{ [key: string]: ReferenceObject | HttpSecurityScheme | ApiKeySecurityScheme | OAuth2SecurityScheme | OpenIdSecurityScheme; }'.
            Property 'bearerAuth' is incompatible with index signature.
              Type '{ type: string; description: string; scheme: string; }' is not assignable to type 'ReferenceObject | HttpSecurityScheme | ApiKeySecurityScheme | OAuth2SecurityScheme | OpenIdSecurityScheme'.
                Type '{ type: string; description: string; scheme: string; }' is not assignable to type 'HttpSecurityScheme'.
                  Types of property 'type' are incompatible.
                    Type 'string' is not assignable to type '"http"'.ts(2345)


显式变量类型修复了没有(错误)强制转换的编译错误:

从“openapi类型”导入{OpenAPIV3};
const API:OpenAPIV3.Document={
// ...
键入:“http”,
// ...
};

看起来类型推断确实有问题。同时验证您的答案,但希望得到更详细的答案,说明原因。@nfroidure typescript不会将文字类型保留太长时间,尤其是在声明对象时。所以它将被提升为一个字符串。但是,在声明变量时传递确切的类型注释将约束字段为特定类型(如示例中的文字类型)。