Giao diện đã nhập xuất khẩu TypeScript


97

Tôi sử dụng các mô-đun AMD và tôi muốn ẩn một giao diện phức tạp đằng sau một tệp tải một số tệp khác và chọn những gì sẽ hiển thị và cách thức. Nó hoạt động, tôi sử dụng giải pháp này nhưng nó cảm thấy hơi xấu, chủ yếu là với các giao diện.

import Types = require('./message-types');
import MessageBaseImport = require('./message-base');
export interface IMessage extends Types.IMessage {} // This is an interface
export var MessageBase = MessageBaseImport; // This is a class

Sử dụng:

import Message = require('message');
import { * } as Message from 'message'; // Or with ES6 style
var mb = new Message.MessageBase(); // Using the class
var msg: Message.IMessage = null; // Using the interface 

Bất kỳ giải pháp tốt hơn ra khỏi đó? Tôi không muốn đặt mọi thứ vào một tệp duy nhất mà tôi muốn vào importmột tệp duy nhất.

Câu trả lời:


177

Có một cú pháp nhập xuất cho các mô-đun kế thừa và một định dạng xuất tiêu chuẩn cho các mô-đun ES6 hiện đại:

// export the default export of a legacy (`export =`) module
export import MessageBase = require('./message-base');

// export the default export of a modern (`export default`) module
export { default as MessageBase } from './message-base';

// when '--isolatedModules' flag is provided it requires using 'export type'.
export type { default as MessageBase } from './message-base';

// export an interface from a legacy module
import Types = require('./message-types');
export type IMessage = Types.IMessage;

// export an interface from a modern module
export { IMessage } from './message-types';

3
Tuy nhiên, đó là bất hợp pháp để sử dụng phong cách này tái xuất trong một không gian tên
— điện tử đám mây

5
Có một lớp lót để xuất và nhập giao diện trong TS tương tự như ví dụ cũ của bạn không? Hay chỉ là trường hợp nhập rồi sử dụng lại cùng một dòng nhưng lại chuyển thành xuất vd. import { IMessage } from './message-types';và sau đó trên dòng tiếp theo cóexport { IMessage } from './message-types';
— mtpultz

1
Tại sao lại là @ e-cloud? Bạn có thể đưa ra một số ví dụ / nền tảng?
— lukas_o

61

Một số ví dụ khác bên cạnh câu trả lời của # c-snover từ đây . Bạn có thể đặt chúng lại với nhau.

import 'jquery';                        // import a module without any import bindings
import $ from 'jquery';                 // import the default export of a module
import { $ } from 'jquery';             // import a named export of a module
import { $ as jQuery } from 'jquery';   // import a named export to a different name
import * as crypto from 'crypto';       // import an entire module instance object

export var x = 42;                      // export a named variable
export function foo() {};               // export a named function

export default 42;                      // export the default export
export default function foo() {};       // export the default export as a function

export { encrypt };                     // export an existing variable
export { decrypt as dec };              // export a variable as a new name
export { encrypt as en } from 'crypto'; // export an export from another module
export * from 'crypto';                 // export all exports from another module
                                        // (except the default export)

Cảm ơn. Nhưng tại sao biến thể nhập cuối cùng lại tách biệt với tất cả các biến thể khác? Nó không phải là ngay dưới bốn người kia?
— Venryx

Nó từ trang web được giới thiệu.
— Gábor Imre


1
Bạn có thể tìm thấy liên kết bị hỏng trong lịch sử của kho lưu trữ: github.com/systemjs/systemjs/blob/…
— Peter

3

Đặc biệt trong trường hợp của tôi, tôi phải 'khai báo' giao diện thay vì xuất nó.

declare interface IFluxStoreSyncOptions{
  namespacedKey: string;
}

Để sử dụng giao diện như một kiểu trong tệp khác như vậy:

export function FluxStoreSync(options: IFluxStoreSyncOptions){
}

Bằng cách này, bạn không phải xuất và nhập giao diện.

Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookie và Chính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.