Posted at

typescriptのimport/export

More than 1 year has passed since last update.


一つだけexport


myclass.ts

export default class MyClass{}



myclass.js

exports.default = class MyClass{}



main.ts

import MyClass from './myclass'

const mc = new MyClass()


複数export


myclass.ts

export class MyClass1 {}

export class MyClass2 {}


myclass.js

exports.MyClass1 = class MyClass1{}

exports.MyClass2 = class MyClass2{}


main.ts

import {MyClass1, MyClass2} from './myclass'

const mc1 = new MyClass1()
const mc2 = new MyClass2()

import * as MyClass from './myclass'
const mc1x = new MyClass.MyClass1()
const mc2x = new MyClass.MyClass2()



node.jsでmodule.exportsをする必要がある場合


myclass.ts

export = class MyClass{}



myclass.js

moudule.exports = class MyClass{}



main.ts

import MyClass = require('./myclass')

const mc = new MyClass()


  • npm等でmodule.exportsをした場合に互換性を維持する場合につかう?