【问题标题】:How to assign types for a variable on the window object in Typescript?如何在 Typescript 中为窗口对象上的变量分配类型?
【发布时间】:2023-02-26 17:32:34
【问题描述】:

我喜欢使用 Bootstrap 但不喜欢 ESM 版本,而是只加载捆绑的普通版本及其全局对象和自动化功能......

所以我安装了npm install @types/bootstrap --save-dev

import type * as bootstrapTypes from 'bootstrap';

declare global {
    interface Window {
        bootstrap: bootstrapTypes;
    }
}

const bs = window.bootstrap;

在 Codium(VSCode wihout M$) 中,将鼠标悬停在 ...ap: bootstrapTypes; 上时会出现错误:

类型 bootstrapTypes = /悬而未决/ any

不能将命名空间“bootstrapTypes”用作 type.ts(2709)

导出接口的属性“bootstrap”具有或正在使用私有名称“bootstrapTypes”.ts(4033)

【问题讨论】:

    标签: typescript bootstrap-5 es6-modules


    【解决方案1】:

    好他妈的,还有那个对我的问题投反对票的白痴。我努力寻找。

    ChatGPT 给了我这个答案:

    import type * as bootstrapTypes from 'bootstrap';
    
    declare global {
      interface Window {
        bootstrap: typeof bootstrapTypes;
      }
    }
    
    const bs = window.bootstrap;
    

    这也会产生错误,但它帮助我想出了这个:

    import type * as npmBS from 'bootstrap';
    
    declare global {
        interface Window {
            bootstrap;
        }
    }
    
    const bs = window.bootstrap as typeof npmBS;
    

    有趣的是,人工智能现在比这个糟糕的网站更有帮助。

    【讨论】:

      猜你喜欢
      • 2017-11-26
      • 2021-09-04
      • 2012-08-31
      • 2015-02-05
      • 2020-05-22
      • 1970-01-01
      • 2019-04-01
      • 1970-01-01
      • 2018-01-02
      相关资源
      最近更新 更多