Skip to content

Imports & Exports

Atscript imports and exports work similar to TypeScript with some specific limitations and rules.

Key Limitations

  1. No default imports/exports - Only named exports and imports are supported
  2. No namespace or rename syntax - No import * as, export * as, or import { a as b }
  3. .as files can only import .as files - Cannot import TypeScript or JavaScript
  4. TypeScript can import .as files - Requires .as.d.ts files (generated by VSCode extension or asc CLI)

Named Exports

atscript
// user.as - Named exports only
export interface User {
    id: string
    name: string
}

export type UserID = string
export type Status = 'active' | 'inactive'

// Private (not exported)
interface InternalConfig {
    debug: boolean
}

Importing in Atscript Files

In .as files, omit the file extension:

atscript
// app.as
import { User, UserID, Status } from './user'
import { Product } from '../models/product'
import { validateEmail } from '../../utils/validators'

export interface Order {
    user: User
    items: Product[]
}

Importing in TypeScript Files

TypeScript files must include the .as extension:

typescript
// app.ts
import { User, UserID, Status } from './user.as'
import { Product } from '../models/product.as'

// Use as both type and runtime object
const user: User = { id: '1', name: 'John' }
const validator = User.validator()
const metadata = User.metadata

Valid Import/Export Examples

Basic Named Import/Export

atscript
// types.as
export interface Person {
    name: string
}

export type ID = string
atscript
// main.as
import { Person, ID } from './types'

export interface Employee extends Person {
    employeeId: ID
}

Multiple Imports from Same File

atscript
// models.as
export interface User { }
export interface Product { }
export interface Order { }
export type Status = string
atscript
// app.as
import { User, Product, Order, Status } from './models'

Nested Directory Imports

atscript
// domain/user.as
import { BaseEntity } from '../shared/base'
import { Address } from './types/address'
import { validate } from '../../utils/validation'

Invalid Syntax (Not Supported)

atscript
// ❌ Default exports
export default interface User { }

// ❌ Default imports
import User from './user'

// ❌ Namespace imports
import * as models from './models'

// ❌ Export namespace
export * as utils from './utils'

// ❌ Import with rename
import { User as UserModel } from './user'

// ❌ Re-exports
export { User } from './user'

// ❌ Importing non-.as files
import { helper } from './helper.ts'

Setup for TypeScript Integration

For TypeScript to import .as files:

  1. With VSCode Extension: Automatically generates .as.d.ts files on save
  2. With CLI: Run asc -f dts to generate TypeScript definitions
  3. With Bundler: Use unplugin-atscript for automatic compilation

Example generated structure:

src/
  user.as          # Source file
  user.as.js       # Generated JavaScript (with asc -f js)
  user.as.d.ts     # Generated TypeScript definitions
  app.ts           # Can import from './user.as'

Next Steps

Released under the ISC License.