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

assumptions.mddocs/

0

# Assumptions

1

2

Babel compatibility assumptions that can be configured to optimize transformations based on how your code behaves. These assumptions allow SWC to generate more optimized output by skipping certain runtime checks when the assumptions are known to be true.

3

4

## Capabilities

5

6

### Assumptions Interface

7

8

Configuration interface for Babel compatibility assumptions, enabling optimized transformations.

9

10

```typescript { .api }

11

/**

12

* Babel compatibility assumptions for optimized transformations

13

* See: https://babeljs.io/docs/en/assumptions

14

*/

15

interface Assumptions {

16

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

17

arrayLikeIsIterable?: boolean;

18

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

19

constantReexports?: boolean;

20

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

21

constantSuper?: boolean;

22

/** Assume module metadata is enumerable */

23

enumerableModuleMeta?: boolean;

24

/** Ignore function length property when transforming */

25

ignoreFunctionLength?: boolean;

26

/** Ignore function name property when transforming */

27

ignoreFunctionName?: boolean;

28

/** Ignore toPrimitive hint parameter */

29

ignoreToPrimitiveHint?: boolean;

30

/** Assume iterable objects are arrays */

31

iterableIsArray?: boolean;

32

/** Assume template objects are mutable */

33

mutableTemplateObject?: boolean;

34

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

35

noClassCalls?: boolean;

36

/** Assume document.all doesn't exist */

37

noDocumentAll?: boolean;

38

/** Skip incomplete namespace import detection */

39

noIncompleteNsImportDetection?: boolean;

40

/** Assume arrow functions are never constructed with new */

41

noNewArrows?: boolean;

42

/** Assume object rest doesn't include symbol properties */

43

objectRestNoSymbols?: boolean;

44

/** Transform private fields as regular properties */

45

privateFieldsAsProperties?: boolean;

46

/** Assume getters have no side effects */

47

pureGetters?: boolean;

48

/** Set class methods as enumerable */

49

setClassMethods?: boolean;

50

/** Set computed properties as enumerable */

51

setComputedProperties?: boolean;

52

/** Set public class fields as enumerable */

53

setPublicClassFields?: boolean;

54

/** Set spread properties as enumerable */

55

setSpreadProperties?: boolean;

56

/** Skip for-of iterator closing */

57

skipForOfIteratorClosing?: boolean;

58

/** Assume super is callable constructor */

59

superIsCallableConstructor?: boolean;

60

/** @deprecated TypeScript enum readonly assumption - always true */

61

tsEnumIsReadonly?: boolean;

62

}

63

```

64

65

**Usage Examples:**

66

67

```typescript

68

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

69

70

// Conservative assumptions for maximum compatibility

71

const safeAssumptions: Assumptions = {

72

constantSuper: false,

73

noClassCalls: false,

74

privateFieldsAsProperties: false,

75

pureGetters: false

76

};

77

78

// Optimized assumptions for modern environments

79

const optimizedAssumptions: Assumptions = {

80

constantSuper: true,

81

noClassCalls: true,

82

noNewArrows: true,

83

privateFieldsAsProperties: true,

84

pureGetters: true,

85

setClassMethods: true,

86

setPublicClassFields: true,

87

arrayLikeIsIterable: true,

88

iterableIsArray: true

89

};

90

91

// Using assumptions in SWC configuration

92

const jscConfig: JscConfig = {

93

target: "es2020",

94

assumptions: optimizedAssumptions,

95

parser: {

96

syntax: "typescript",

97

tsx: true

98

}

99

};

100

```

101

102

### Array and Iteration Assumptions

103

104

Assumptions related to array-like objects and iteration behavior.

105

106

```typescript { .api }

107

interface ArrayIterationAssumptions {

108

/** Assume array-like objects implement Symbol.iterator */

109

arrayLikeIsIterable?: boolean;

110

/** Assume iterable objects are always arrays */

111

iterableIsArray?: boolean;

112

/** Skip cleanup when for-of loops terminate early */

113

skipForOfIteratorClosing?: boolean;

114

}

115

```

116

117

### Class and Object Assumptions

118

119

Assumptions about class behavior and object property handling.

120

121

```typescript { .api }

122

interface ClassObjectAssumptions {

123

/** Assume classes are never invoked without new */

124

noClassCalls?: boolean;

125

/** Assume arrow functions are never constructed */

126

noNewArrows?: boolean;

127

/** Transform private fields as regular properties */

128

privateFieldsAsProperties?: boolean;

129

/** Set class methods as enumerable properties */

130

setClassMethods?: boolean;

131

/** Set public class fields as enumerable */

132

setPublicClassFields?: boolean;

133

/** Set computed properties as enumerable */

134

setComputedProperties?: boolean;

135

/** Set spread properties as enumerable */

136

setSpreadProperties?: boolean;

137

/** Assume object rest doesn't include symbols */

138

objectRestNoSymbols?: boolean;

139

}

140

```

141

142

### Module and Function Assumptions

143

144

Assumptions about module behavior and function properties.

145

146

```typescript { .api }

147

interface ModuleFunctionAssumptions {

148

/** Assume re-exported bindings don't change */

149

constantReexports?: boolean;

150

/** Assume module metadata is enumerable */

151

enumerableModuleMeta?: boolean;

152

/** Skip incomplete namespace import detection */

153

noIncompleteNsImportDetection?: boolean;

154

/** Ignore function.length when transforming */

155

ignoreFunctionLength?: boolean;

156

/** Ignore function.name when transforming */

157

ignoreFunctionName?: boolean;

158

}

159

```

160

161

### Runtime Behavior Assumptions

162

163

Assumptions about runtime behavior and built-in operations.

164

165

```typescript { .api }

166

interface RuntimeAssumptions {

167

/** Assume super class constructor doesn't change */

168

constantSuper?: boolean;

169

/** Assume super is always a callable constructor */

170

superIsCallableConstructor?: boolean;

171

/** Assume getters have no side effects */

172

pureGetters?: boolean;

173

/** Assume document.all doesn't exist */

174

noDocumentAll?: boolean;

175

/** Ignore toPrimitive hint parameter */

176

ignoreToPrimitiveHint?: boolean;

177

/** Assume template objects can be mutated */

178

mutableTemplateObject?: boolean;

179

}

180

```

181

182

## Performance Impact

183

184

Enabling assumptions can significantly improve transformation performance and output size:

185

186

- **Memory Usage**: Reduced by avoiding defensive checks

187

- **Bundle Size**: Smaller due to simplified generated code

188

- **Runtime Performance**: Faster execution with fewer indirections

189

- **Compatibility Risk**: Higher chance of runtime errors if assumptions are violated

190

191

## Migration from Babel

192

193

When migrating from Babel, map your existing assumptions configuration directly:

194

195

```typescript

196

// Babel assumptions in .babelrc

197

{

198

"assumptions": {

199

"constantSuper": true,

200

"noClassCalls": true

201

}

202

}

203

204

// Equivalent SWC configuration

205

const swcConfig = {

206

jsc: {

207

assumptions: {

208

constantSuper: true,

209

noClassCalls: true

210

}

211

}

212

};

213

```