or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

assumptions.mdast-nodes.mdconfiguration.mdindex.mdjsx.mdminification.mdmodules.mdparser.mdtypescript.md

index.mddocs/

0

# @swc/types

1

2

@swc/types provides comprehensive TypeScript type definitions for @swc/core APIs, serving as a lightweight dependency for tools that need SWC type information without depending on the full @swc/core package. It contains extensive type definitions for JavaScript minification options (including Terser-compatible interfaces), parsing configurations, AST node types, plugin interfaces, and compilation assumptions.

3

4

## Package Information

5

6

- **Package Name**: @swc/types

7

- **Package Type**: npm

8

- **Language**: TypeScript

9

- **Installation**: `npm install @swc/types`

10

11

## Core Imports

12

13

```typescript

14

import type {

15

Options, Config, JscConfig, ModuleConfig,

16

Plugin, ParseOptions, JsMinifyOptions,

17

// AST types

18

Program, Module, Statement, Expression,

19

// JSX types

20

JSXElement, JSXFragment, JSXElementChild,

21

// TypeScript types

22

TsType, TsInterfaceDeclaration,

23

// Babel assumptions

24

Assumptions

25

} from "@swc/types";

26

```

27

28

CommonJS:

29

30

```javascript

31

const { Options, Config } = require("@swc/types");

32

```

33

34

## Basic Usage

35

36

```typescript

37

import type { Options, JscConfig, ModuleConfig } from "@swc/types";

38

39

// SWC compilation configuration with JSX support

40

const swcOptions: Options = {

41

jsc: {

42

target: "es2020",

43

parser: {

44

syntax: "typescript",

45

tsx: true,

46

decorators: true

47

},

48

transform: {

49

react: {

50

runtime: "automatic"

51

}

52

},

53

assumptions: {

54

constantSuper: true,

55

noClassCalls: true,

56

privateFieldsAsProperties: true

57

}

58

},

59

module: {

60

type: "es6"

61

},

62

minify: true

63

};

64

65

// Minification configuration

66

const minifyOptions: JsMinifyOptions = {

67

compress: {

68

dead_code: true,

69

drop_console: true

70

},

71

mangle: {

72

topLevel: true

73

}

74

};

75

```

76

77

## Architecture

78

79

@swc/types is organized around several key type categories:

80

81

- **Configuration Types**: Interfaces for configuring SWC compilation, parsing, and transformation

82

- **AST Node Types**: Complete JavaScript/TypeScript abstract syntax tree definitions

83

- **TypeScript Support**: TypeScript-specific syntax and type system representations

84

- **Minification Types**: Terser-compatible interfaces for code optimization

85

- **Module Systems**: Type definitions for various module formats (ES6, CommonJS, UMD, AMD)

86

- **Plugin System**: WebAssembly plugin interfaces and configuration types

87

88

## Capabilities

89

90

### Configuration and Options

91

92

Core configuration interfaces for SWC compilation, parsing, and transformation. Essential for any tool integrating with SWC.

93

94

```typescript { .api }

95

interface Options extends Config {

96

script?: boolean;

97

cwd?: string;

98

filename?: string;

99

root?: string;

100

rootMode?: "root" | "upward" | "upward-optional";

101

}

102

103

interface Config {

104

test?: string | string[];

105

exclude?: string | string[];

106

env?: EnvConfig;

107

jsc?: JscConfig;

108

module?: ModuleConfig;

109

minify?: boolean;

110

sourceMaps?: boolean | "inline";

111

}

112

113

interface JscConfig {

114

assumptions?: Assumptions;

115

parser?: ParserConfig;

116

transform?: TransformConfig;

117

target?: JscTarget;

118

minify?: JsMinifyOptions;

119

}

120

```

121

122

[Configuration](./configuration.md)

123

124

### AST Node Types

125

126

Complete abstract syntax tree type definitions covering all JavaScript and TypeScript syntax elements. Used for AST manipulation and analysis.

127

128

```typescript { .api }

129

type Program = Module | Script;

130

131

interface Module extends Node, HasSpan {

132

type: "Module";

133

body: ModuleItem[];

134

}

135

136

type Statement =

137

| BlockStatement | EmptyStatement | DebuggerStatement

138

| WithStatement | ReturnStatement | LabeledStatement

139

| BreakStatement | ContinueStatement | IfStatement

140

| SwitchStatement | ThrowStatement | TryStatement

141

| WhileStatement | DoWhileStatement | ForStatement

142

| ForInStatement | ForOfStatement | Declaration

143

| ExpressionStatement;

144

145

type Expression =

146

| ThisExpression | ArrayExpression | ObjectExpression

147

| FunctionExpression | UnaryExpression | UpdateExpression

148

| BinaryExpression | AssignmentExpression | MemberExpression

149

| ConditionalExpression | CallExpression | NewExpression

150

| Identifier | Literal | TemplateLiteral

151

| ArrowFunctionExpression | ClassExpression | YieldExpression

152

| AwaitExpression | OptionalChainingExpression;

153

```

154

155

[AST Nodes](./ast-nodes.md)

156

157

### TypeScript Support

158

159

TypeScript-specific AST nodes and type system representations for TypeScript compilation and analysis.

160

161

```typescript { .api }

162

type TsType =

163

| TsKeywordType | TsThisType | TsFnOrConstructorType

164

| TsTypeReference | TsTypeQuery | TsTypeLiteral

165

| TsArrayType | TsTupleType | TsOptionalType

166

| TsRestType | TsUnionOrIntersectionType

167

| TsConditionalType | TsInferType | TsParenthesizedType

168

| TsTypeOperator | TsIndexedAccessType | TsMappedType

169

| TsLiteralType | TsTypePredicate | TsImportType;

170

171

interface TsInterfaceDeclaration extends Node, HasSpan {

172

type: "TsInterfaceDeclaration";

173

id: Identifier;

174

declare: boolean;

175

typeParams?: TsTypeParameterDeclaration;

176

extends: TsExpressionWithTypeArguments[];

177

body: TsInterfaceBody;

178

}

179

180

interface TsTypeAliasDeclaration extends Node, HasSpan {

181

type: "TsTypeAliasDeclaration";

182

declare: boolean;

183

id: Identifier;

184

typeParams?: TsTypeParameterDeclaration;

185

typeAnnotation: TsType;

186

}

187

```

188

189

[TypeScript Support](./typescript.md)

190

191

### Minification Options

192

193

Terser-compatible interfaces for JavaScript minification and code optimization.

194

195

```typescript { .api }

196

interface JsMinifyOptions {

197

compress?: TerserCompressOptions | boolean;

198

format?: JsFormatOptions;

199

mangle?: TerserMangleOptions | boolean;

200

ecma?: TerserEcmaVersion;

201

keep_classnames?: boolean;

202

keep_fnames?: boolean;

203

module?: boolean | "unknown";

204

sourceMap?: boolean;

205

}

206

207

interface TerserCompressOptions {

208

arguments?: boolean;

209

arrows?: boolean;

210

booleans?: boolean;

211

dead_code?: boolean;

212

drop_console?: boolean;

213

drop_debugger?: boolean;

214

evaluate?: boolean;

215

// ... extensive compression options

216

}

217

```

218

219

[Minification](./minification.md)

220

221

### Module Systems

222

223

Type definitions for various JavaScript module systems and their configuration options.

224

225

```typescript { .api }

226

type ModuleConfig =

227

| Es6Config | CommonJsConfig | UmdConfig

228

| AmdConfig | NodeNextConfig | SystemjsConfig;

229

230

interface BaseModuleConfig {

231

strict?: boolean;

232

strictMode?: boolean;

233

lazy?: boolean | string[];

234

importInterop?: "swc" | "babel" | "node" | "none";

235

}

236

237

interface Es6Config extends BaseModuleConfig {

238

type: "es6";

239

}

240

241

interface CommonJsConfig extends BaseModuleConfig {

242

type: "commonjs";

243

}

244

```

245

246

[Module Systems](./modules.md)

247

248

### Parser Configuration

249

250

Parser configuration for JavaScript and TypeScript syntax support.

251

252

```typescript { .api }

253

type ParseOptions = ParserConfig & {

254

comments?: boolean;

255

script?: boolean;

256

target?: JscTarget;

257

};

258

259

type ParserConfig = TsParserConfig | EsParserConfig;

260

261

interface TsParserConfig {

262

syntax: "typescript";

263

tsx?: boolean;

264

decorators?: boolean;

265

}

266

267

interface EsParserConfig {

268

syntax: "ecmascript";

269

jsx?: boolean;

270

decorators?: boolean;

271

// ... additional ES feature flags

272

}

273

```

274

275

[Parser Configuration](./parser.md)

276

277

### JSX Support

278

279

Complete JSX/React AST node definitions for parsing and transforming JSX syntax. Essential for React applications and JSX-based frameworks.

280

281

```typescript { .api }

282

interface JSXElement extends Node, HasSpan {

283

type: "JSXElement";

284

opening: JSXOpeningElement;

285

children: JSXElementChild[];

286

closing?: JSXClosingElement;

287

}

288

289

interface JSXFragment extends Node, HasSpan {

290

type: "JSXFragment";

291

opening: JSXOpeningFragment;

292

children: JSXElementChild[];

293

closing: JSXClosingFragment;

294

}

295

296

type JSXElementName =

297

| Identifier // <Button>

298

| JSXMemberExpression // <UI.Button>

299

| JSXNamespacedName; // <svg:circle>

300

301

type JSXElementChild =

302

| JSXText | JSXExpressionContainer

303

| JSXSpreadChild | JSXElement | JSXFragment;

304

```

305

306

[JSX Support](./jsx.md)

307

308

### Babel Assumptions

309

310

Optimization assumptions for Babel compatibility, enabling more efficient transformations when code behavior is predictable.

311

312

```typescript { .api }

313

interface Assumptions {

314

/** Assume array-like objects are iterable */

315

arrayLikeIsIterable?: boolean;

316

/** Assume re-exported bindings are constant */

317

constantReexports?: boolean;

318

/** Assume super class doesn't change at runtime */

319

constantSuper?: boolean;

320

/** Assume classes are never called as functions */

321

noClassCalls?: boolean;

322

/** Transform private fields as regular properties */

323

privateFieldsAsProperties?: boolean;

324

/** Assume getters have no side effects */

325

pureGetters?: boolean;

326

// ... 17+ additional optimization flags

327

}

328

```

329

330

[Babel Assumptions](./assumptions.md)

331

332

## Types

333

334

```typescript { .api }

335

interface Node {

336

type: string;

337

}

338

339

interface HasSpan {

340

span: Span;

341

}

342

343

interface Span {

344

start: number;

345

end: number;

346

ctxt: number;

347

}

348

349

type JscTarget =

350

| "es3" | "es5" | "es2015" | "es2016" | "es2017"

351

| "es2018" | "es2019" | "es2020" | "es2021"

352

| "es2022" | "es2023" | "es2024" | "esnext";

353

354

type TerserEcmaVersion = 5 | 2015 | 2016 | string | number;

355

356

interface Output {

357

code: string;

358

map?: string;

359

}

360

```