代码之家  ›  专栏  ›  技术社区  ›  ar_IGT

src/app/providers中存在错误。ts:152:36-错误TS2339:类型“Window”上不存在属性“API”-TypeScript自定义类型错误

  •  0
  • ar_IGT  · 技术社区  · 3 年前

    用Angular 8和TypeScript 3.4.5编写应用程序。

    使用运行本地Angular开发服务器时 ng server ,我收到阻止编译的错误:

    ERROR in src/app/providers.ts:152:36 - error TS2339: Property 'API' does not exist on type 'Window'.
    
    152   { provide: API, useValue: window.API },
                                           ~~~
    src/environments/environment.static.ts:18:8 - error TS2339: Property 'FooServices' does not exist on type 'Window'.
    
    18 parent.FooServices.Component = (): { subscribe: () => void } => {
              ~~~~~~~~~~~~~~
    src/zone-flags.ts:1:8 - error TS2339: Property '__Zone_disable_requestAnimationFrame' does not exist on type 'Window'.
    
    1 window.__Zone_disable_requestAnimationFrame = true; // disable patch requestAnimationFrame
    

    我想的是,为标准浏览器创建自定义的全局类型扩展 Window 对象

    所以我制作了自定义的全局类型:

    declare global {
      interface Date {
        format(value: string): string;
      }
    
      interface Window {
        __Zone_disable_on_property: any;
        __Zone_disable_requestAnimationFrame: any;
        __Zone_disable_XHR: any;
        __Zone_ignore_on_properties: any;
        __zone_symbol__BLACK_LISTED_EVENTS: any;
        API: any;
        BUS: any;
        bus: any;
        config: any;
        ContentConfig: any;
        ContentManager: any;
        ContentQueue: any;
        DataRegistry: any;
        global: any;
        hostMessageSender: any;
        LanguageUtils: any;
        lock: any;
        logger: any;
        mdiManager: any;
        METER: any;
        msg: any;
        FooServices: any;
        FooConfig: any;
        RegistryUtils: any;
        serviceMenuController: any;
        XMLHttpRequest: any;
      }
    }
    
    export {};
    

    这是我的 tsconfig.json 文件:

    {
      "compileOnSave": false,
      "compilerOptions": {
        "baseUrl": "./",
        "downlevelIteration": true,
        "outDir": "./dist/out-tsc",
        "sourceMap": false,
        "declaration": false,
        "module": "amd",
        "moduleResolution": "node",
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "importHelpers": true,
        "target": "es5",
        "noImplicitAny": false,
        "typeRoots": [
          "projects/shared/src/lib/types",
          "node_modules/@types"
        ],
        "lib": ["es2018", "dom"],
        "resolveJsonModule": true
      }
    }
    

    理论上,这个配置应该有效,因为中的自定义类型 user_interface/projects/shared/src/lib/types/global.d.ts 文件正确包含在上述配置中。

    但我的IntelliJ IDEA Ultimate仍然不断指出一些文件中的类型错误,这些文件使用 parent.FooServices 或 window.FooConfig 等等对象。

    根据这篇短文 How to declare global types in TypeScript ,这种解决方案应该可以正常工作。

    我真的不知道我错过了什么,也不知道我在哪里误判了。

    我通过SO看到的是,许多人不时地在自定义打字员方面遇到同样的问题。

    1 回复  |  直到 3 年前
        1
  •  0
  •   Kirill Kushpel    3 年前

    你试着添加了吗 include 字段给您tsconfig?

    "include": [
      'projects/shared/src/lib/types/global.d.ts' // the path must be relevant to the config location
    ]
    

    enter image description here

        2
  •  0
  •   ar_IGT    3 年前

    我找到了这个问题和类似问题的解决方案,我在回答我的问题时对此进行了描述 I'm getting the error in ./src/main.ts file Module not found: Can't resolve types while compiling Angular app with ng serve .

    推荐文章