This specification defines a notation format for representing TypeScript imports and exports. It is designed to be concise, flexible, and easily understandable, suitable for use in various contexts including configuration files, command-line interfaces, and documentation.
Basic Structure
- Imports and Exports: Use
-to start import declarations and~to start export declarations. - Namespace Imports: Double bangs
!!indicate namespace imports (e.g.,* as name). - Type-only Imports/Exports: Double tilde
~~for type-only exports and double dash--for type-only imports. - Grouping: Use parentheses
()to group the module specifier with its imports or exports.
Detailed Syntax:
- Namespace Import:
-moduleName(!!alias)- Example:
-transferables(!!transferables)
- Example:
- Default Import:
-moduleName(alias)- Example:
-spring-easing(transfer)
- Example:
- Named Import/Export:
-moduleName(alias1!alias2)alias1is the original name, andalias2is the local name.- Example:
-spring-easing(SpringEasing!SE)
- Type Import:
--moduleName(TypeName)- Example:
--spring-easing(SpringEasingType)
- Example:
- Type Export:
~~moduleName(TypeName)- Example:
~~spring-easing(SpringEasingType)
- Example:
- Namespace Export:
~moduleName(*!alias)- Example:
~transferables(*!ttf)
- Example:
- Aggregate Exports:
moduleName(!exported1~type*!exported2~type*exported3!alias)- Combines multiple export forms, including renaming and type exports.
- Example:
spring-easing(!SpringEasing~type*!SpringFrame~type*SpringOutFrame!SpringOutFrameType~SpringOutFrame!Spring)
This format takes inspiration from the TypeScript Handbook on Modules (TypeScript Modules) and ECMAScript modules specifications. It aims to provide an analogous notation that encapsulates the flexibility of TypeScript's rich import/export syntax.
-
Namespace Import and Export
- Input:
-lodash(!!_)~d3(*!d3) - Expected Interpretation: Import all of lodash as
_and export all of d3 asd3.
- Input:
-
Complex Aggregate Export
- Input:
~utils(!combine~type*!merge~type*select!selectAs) - Expected Interpretation: Export
combineandmergeas types, andselectasselectAsfrom the "utils" module.
- Input:
-
Type-only Import and Export
- Input:
--colors(ColorType)~~animations(AnimationType) - Expected Interpretation: Import
ColorTypefrom "colors" module and exportAnimationTypefrom "animations" module as a type.
- Input:
-
Mixed Import Types
- Input:
-rxjs(observable!Observable)--rxjs(operatorType) - Expected Interpretation: Import
observableasObservableand importoperatorTypeas a type from "rxjs".
- Input:
-
Default and Named Imports
- Input:
-axios(default!axios)-axios(config!AxiosConfig) - Expected Interpretation: Import the default export of "axios" as
axiosandconfigasAxiosConfig.
- Input:
Revised Specification for TypeScript Import and Export Notation (CTIE Format) with Emphasis on Type-Only Imports and Exports
This updated specification for the CTIE format places a stronger emphasis on promoting type-only imports and exports where applicable. The goal is to encourage the separation of type information from runtime code, enhancing code optimization and clarity. This approach aligns with modern TypeScript practices that aim to keep runtime code and type definitions distinctly managed, thus facilitating better build optimization and cleaner code separation.
Basic Structure Adjustments
- Type-Only Imports and Exports Preference: Whenever possible, type-only imports and exports should be used to separate type definitions from runtime imports/exports.
- Syntax Highlighting for Types:
--is used for type-only imports and~~for type-only exports to distinctly separate them from runtime code interactions.
Detailed Syntax:
- Type Import:
--moduleName(TypeName)- Example:
--react(ReactComponentType)
- Example:
- Type Export:
~~moduleName(TypeName)- Example:
~~utils(UtilityTypes)
- Example:
To help clarify how the CTIE notation maps to TypeScript code, the following examples illustrate the notation alongside the equivalent TypeScript code. This section aims to demonstrate practical usage scenarios and ensure the specification's applicability and ease of understanding.
CTIE Format:
-transferables(!!transferables)~lodash(*!_)
TypeScript Code:
import * as transferables from "transferables";
export * as _ from "lodash";Description:
- The CTIE notation
-transferables(!!transferables)indicates importing all of "transferables" as an aliastransferables. - The notation
~lodash(*!_)indicates exporting all of "lodash" under the alias_.
CTIE Format:
-react(default!React)-react-dom(ReactDOM)
TypeScript Code:
import React from "react";
import ReactDOM from "react-dom";Description:
default!Reactspecifies importing the default export from "react" and naming itReact.ReactDOMwithout any prefixes implies importing the default export asReactDOM.
CTIE Format:
~utils(!combine~type*!merge~type*select!selectAs)
TypeScript Code:
export { combine, type merge, select as selectAs } from "utils";Description:
- This exports
combineas a regular export,mergeas a type, and renamesselecttoselectAs.
CTIE Format:
--colors(ColorType)~~animations(AnimationType)
TypeScript Code:
import type { ColorType } from "colors";
export type { AnimationType } from "animations";Description:
- The
--colors(ColorType)notation specifies importingColorTypeas a type-only import. - The
~~animations(AnimationType)notation specifies exportingAnimationTypeas a type-only export.
CTIE Format:
-rxjs(observable!Observable)--rxjs(operatorType)~rxjs(!Subject~type*!BehaviorSubject)
TypeScript Code:
import { observable as Observable, type operatorType } from "rxjs";
export { Subject, type BehaviorSubject } from "rxjs";Description:
- Import
observablefrom "rxjs" and rename it toObservable. - Import
operatorTypeas a type-only import. - Export
SubjectandBehaviorSubjectas a type from "rxjs".
-
Prefer Type-Only Imports/Exports: When both type and runtime values are available from a module, prefer using type-only imports/exports unless the runtime value is explicitly required.
-
Enforce Type-Only Where Applicable: In scenarios where a type is only used for type checking (e.g., interfaces, types), enforce the usage of type-only imports and exports.
-
Mixed Imports/Exports Handling: For modules providing both types and values, separate imports and exports clearly using the designated syntax for types and values.
CTIE Format:
--material-ui(styles)
TypeScript Code:
import type { styles } from "material-ui";Description:
- This imports
stylesas a type-only import, emphasizing that it's used for type checking and not included in the runtime bundle.
CTIE Format:
~~helpers(APIResponseTypes)
TypeScript Code:
export type { APIResponseTypes } from "helpers";Description:
- This exports
APIResponseTypesas a type-only export, making it clear that these are only for type checking.
CTIE Format:
-react(React)--react(ReactComponentProps)
TypeScript Code:
import React from "react";
import type { ReactComponentProps } from "react";Description:
- Regular import for the React object, and a type-only import for
ReactComponentProps.
These examples are designed to show the versatility and expressiveness of the CTIE notation in handling various import and export scenarios. They demonstrate how a single line of CTIE notation can represent complex TypeScript module interactions, facilitating a clear, concise, and scalable approach to managing TypeScript imports and exports.
- Comprehensive Documentation: Provide detailed documentation with examples for each type of import and export.
- Visual Aids: Include diagrams or flowcharts in documentation to visualize how the notation maps to TypeScript code.
- Tools and Linters: Develop tools or extend existing linters to validate and auto-correct the notation as per the specification.
- Interactive Learning Modules: Offer interactive tutorials or a playground to experiment with the notation and see real-time TypeScript code generation.
- Tooling Support: Enhance tooling to recognize and differentiate between type-only and regular imports/exports automatically, suggesting adjustments when possible.
- Linting and Code Review: Integrate lint rules that favor type-only imports and exports during code reviews to enforce this practice.
- Documentation and Training: Provide detailed documentation and training materials that emphasize the importance and benefits of using type-only imports and exports.
This format aims to standardize the representation of module import and export operations in a compact and expressive manner, facilitating ease of use across different platforms and tools.