Typescript 导入 - 每个文件约定一个 class
Typescript import - One class per file convention
我刚刚开始使用 Typescript 进行前端 Web 项目。我的目标是使用 OOP 方法。所以我的约定是一个 class 按文件(从它包含的 class 命名)。
考虑这个例子:
//Car.ts
export default class Car {
public startEngine (): void {
console.log('vroom!');
}
}
//RaceCar.ts
import Car from './Car';
export default class RaceCar extends Car {
public startEngine (): void {
console.log('!!VROOOOOMM!!');
}
}
//index.ts
import RaceCar from './RaceCar';
import Car from './RaceCar';
const car:Car = new RaceCar();
car.startEngine();
此代码工作正常,但我有 2 个问题:
- 当我使用约定 "one class per file"(默认导出)时,写
import Car from './Car';
真的很烦人。有没有捷径可以做到这一点?比如宏之类的?我试过 import './Car';
但当然这不会导入 Car
符号...
- 另一个烦人的事情是在index.ts中导入Car和RaceCar。有没有办法只导入已经知道 Race class?
的 RaceCar
换句话说,我正在寻找一种方法来执行类似 C++ 包含的操作。
TypeScript 使用 +ES6 语法:
根据https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import
Import an entire module for side effects only, without importing
anything. This runs the module's global code, but doesn't actually
import any values.
import '/modules/my-module.js';
您需要导入您要使用的符号:
import myDefault from '/modules/my-module.js'; // myDefault is the default exported symbol in the module.
因此,没有。您始终需要明确地导入要使用的符号,并且只能从需要使用它们的这些代码位置导入。
When I use the convention "one class per file" (with default export) it is really annoying to write import Car from './Car';. Is there a short way to do this? Like a macro or something? I have tried import './Car'; but of course this does not import Car symbol...
要使用汽车,代码必须说 import Car from './Car'
。这种语法基本上意味着 "run the code in ./Car, and assign its default export to Car
"。 import './Car'
是合法的,但它意味着 "run the code in ./Car, but i don't need to use what it exports"。
也就是说,各种 IDE 可以帮助您自动填充导入。例如,我使用 Visual Studio 代码,如果我在我的代码中某处使用 Car
而没有导入或定义它,我会得到红色下划线,然后可以按 ctrl-space 自动导入在大多数情况下。
Another annoying thing is to import Car and RaceCar in index.ts. Is there a way to import only RaceCar that already knows the Race class?
如果您需要直接引用 Car 文件导出的任何内容,则需要将其导入。在您的示例中,您使用 Car 作为一种类型。如果你需要它,那么你需要导入它。也就是说,在您的情况下,我可能只使用 RaceCar 作为类型,因为这就是您要更新的类型。
import RaceCar from './RaceCar';
const car: RaceCar = new RaceCar();
car.startEngine();
我刚刚开始使用 Typescript 进行前端 Web 项目。我的目标是使用 OOP 方法。所以我的约定是一个 class 按文件(从它包含的 class 命名)。
考虑这个例子:
//Car.ts
export default class Car {
public startEngine (): void {
console.log('vroom!');
}
}
//RaceCar.ts
import Car from './Car';
export default class RaceCar extends Car {
public startEngine (): void {
console.log('!!VROOOOOMM!!');
}
}
//index.ts
import RaceCar from './RaceCar';
import Car from './RaceCar';
const car:Car = new RaceCar();
car.startEngine();
此代码工作正常,但我有 2 个问题:
- 当我使用约定 "one class per file"(默认导出)时,写
import Car from './Car';
真的很烦人。有没有捷径可以做到这一点?比如宏之类的?我试过import './Car';
但当然这不会导入Car
符号... - 另一个烦人的事情是在index.ts中导入Car和RaceCar。有没有办法只导入已经知道 Race class? 的 RaceCar
换句话说,我正在寻找一种方法来执行类似 C++ 包含的操作。
TypeScript 使用 +ES6 语法:
根据https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import
Import an entire module for side effects only, without importing anything. This runs the module's global code, but doesn't actually import any values.
import '/modules/my-module.js';
您需要导入您要使用的符号:
import myDefault from '/modules/my-module.js'; // myDefault is the default exported symbol in the module.
因此,没有。您始终需要明确地导入要使用的符号,并且只能从需要使用它们的这些代码位置导入。
When I use the convention "one class per file" (with default export) it is really annoying to write import Car from './Car';. Is there a short way to do this? Like a macro or something? I have tried import './Car'; but of course this does not import Car symbol...
要使用汽车,代码必须说 import Car from './Car'
。这种语法基本上意味着 "run the code in ./Car, and assign its default export to Car
"。 import './Car'
是合法的,但它意味着 "run the code in ./Car, but i don't need to use what it exports"。
也就是说,各种 IDE 可以帮助您自动填充导入。例如,我使用 Visual Studio 代码,如果我在我的代码中某处使用 Car
而没有导入或定义它,我会得到红色下划线,然后可以按 ctrl-space 自动导入在大多数情况下。
Another annoying thing is to import Car and RaceCar in index.ts. Is there a way to import only RaceCar that already knows the Race class?
如果您需要直接引用 Car 文件导出的任何内容,则需要将其导入。在您的示例中,您使用 Car 作为一种类型。如果你需要它,那么你需要导入它。也就是说,在您的情况下,我可能只使用 RaceCar 作为类型,因为这就是您要更新的类型。
import RaceCar from './RaceCar';
const car: RaceCar = new RaceCar();
car.startEngine();