import “type” and “different variable” in one single line

huangapple go评论70阅读模式
英文:

import "type" and "different variable" in one single line

问题

我是新手 TypeScript。目前,我正在尝试在 Electron 应用程序中使用 TypeScript(只是练习 TypeScript)。

在我的应用程序中,我需要从 electron 包中导入一个“type”和一个“variable/class”,但目前我是这样做的:

import { dialog } from 'electron';
import type { BrowserWindowConstructorOptions } from 'electron';

有没有办法让它成为一行,同时仍然清楚地显示我正在导入的内容(“type”或“variable/class”)?

我知道我可以像这样做:

import { dialog, BrowserWindowConstructorOptions } from 'electron';

但使用上面的代码,不清楚我正在导入的是什么,很难判断我导入的“dialog”是一个类型还是一个变量。

英文:

I am new to Typescript. Currently I am trying to use TypeScript in an electron app(just practice TypeScript).

In my app I need to import a "type" and a "variable/class" in to my app from electron package, but currently I am doing it this way:

import { dialog } from 'electron';
import type { BrowserWindowConstructorOptions } from 'electron';

Is there a way I can make it one line while still clearly show what I am importing(a "type" or a "variable/class")?

I know that I can do something like this:

import { dialog ,BrowserWindowConstructorOptions } from 'electron';

but with above code, it's not clear what I am importing, it's hard to tell if the "dialog" I am importing is a type or a variable.

答案1

得分: 7

你可以在所选的导入之前使用 type 关键字来指定类型:

import { dialog, type BrowserWindowConstructorOptions } from 'electron';

这在 TypeScript 文档的 Modules > Importing Types 部分有详细说明。

英文:

To specify type for selected imports, you may use type keyword before the imported name:

import { dialog, type BrowserWindowConstructorOptions } from 'electron';

This is documented under Modules > Importing Types section in TypeScript docs.

huangapple
  • 本文由 发表于 2023年2月10日 14:13:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75407523.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定