簡體   English   中英

如何擴展 package 類型?

[英]How to extend package types?

嘗試擴展 Electron應用程序

import { app } from "electron"

app.foo = "bar" // Property 'foo' does not exist on type 'App'.ts(2339)

如果我使用//@ts-ignore上面的作品。

嘗試設置.d.ts覆蓋但我想出的方法不起作用(剛開始使用覆蓋)。

electron.d.ts

import { App as ElectronApp } from "electron"

declare module Electron {
  export interface App extends ElectronApp {
    inspect: boolean
  }
}

聲明合並是我一直在尋找的。

electron.d.ts

declare namespace Electron {
  export interface App {
    inspect: boolean
  }
}

您可以以私人名稱導入app ,然后在內部將其類型轉換為您喜歡的任何名稱。

import { app as _app } from "electron"

interface MyCustomProperties {
  foo: string;
}

const app = _app as (typeof _app & MyCustomProperties);
app.foo = "bar";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM