简体   繁体   中英

how to have a class and superclass in two files (Typescript)

How do I split a class + subclass across two files in TypeScript?

// MongoModel.js
class MongoModel {
   ...
}
export = MongoModel;

then in another file:

import MongoModel = require("./MongoModel");

but this is giving error File ....MongoModel.ts is not a module

Do i need to use some module syntax to bundle them together, like a Java package?

The version you used for exporting and importing modules work when you target es5 in your compiler configuration.

When targeting es6 , you have the following ways of exporting/importing modules:

// in MongoClass.ts
export class MongoClass {
    // ... code here
}

// and in other file
import {MongoClass} from '/path/to/MongoClass';

or you can use default export ;

// in MongoClass.ts
export default class MongoClass { ... }
export const somethingElse = 5;

// and import in some other file
// note that MongoClass can be renamed when is exported as default exported member
import BaseMongo from '/path/to/MongoClass';

// this cannot be renamed when importing
import {somethingElse} from '/path/to/MongoClass'; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM