CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-swc--types

Comprehensive TypeScript type definitions for the SWC JavaScript/TypeScript transformation and minification APIs.

Pending
Overview
Eval results
Files

assumptions.mddocs/

Assumptions

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.

Capabilities

Assumptions Interface

Configuration interface for Babel compatibility assumptions, enabling optimized transformations.

/**
 * Babel compatibility assumptions for optimized transformations
 * See: https://babeljs.io/docs/en/assumptions
 */
interface Assumptions {
  /** Assume array-like objects are iterable */
  arrayLikeIsIterable?: boolean;
  /** Assume re-exported bindings are constant */
  constantReexports?: boolean;
  /** Assume super class doesn't change at runtime */
  constantSuper?: boolean;
  /** Assume module metadata is enumerable */
  enumerableModuleMeta?: boolean;
  /** Ignore function length property when transforming */
  ignoreFunctionLength?: boolean;
  /** Ignore function name property when transforming */
  ignoreFunctionName?: boolean;
  /** Ignore toPrimitive hint parameter */
  ignoreToPrimitiveHint?: boolean;
  /** Assume iterable objects are arrays */
  iterableIsArray?: boolean;
  /** Assume template objects are mutable */
  mutableTemplateObject?: boolean;
  /** Assume classes are never called as functions */
  noClassCalls?: boolean;
  /** Assume document.all doesn't exist */
  noDocumentAll?: boolean;
  /** Skip incomplete namespace import detection */
  noIncompleteNsImportDetection?: boolean;
  /** Assume arrow functions are never constructed with new */
  noNewArrows?: boolean;
  /** Assume object rest doesn't include symbol properties */
  objectRestNoSymbols?: boolean;
  /** Transform private fields as regular properties */
  privateFieldsAsProperties?: boolean;
  /** Assume getters have no side effects */
  pureGetters?: boolean;
  /** Set class methods as enumerable */
  setClassMethods?: boolean;
  /** Set computed properties as enumerable */
  setComputedProperties?: boolean;
  /** Set public class fields as enumerable */
  setPublicClassFields?: boolean;
  /** Set spread properties as enumerable */
  setSpreadProperties?: boolean;
  /** Skip for-of iterator closing */
  skipForOfIteratorClosing?: boolean;
  /** Assume super is callable constructor */
  superIsCallableConstructor?: boolean;
  /** @deprecated TypeScript enum readonly assumption - always true */
  tsEnumIsReadonly?: boolean;
}

Usage Examples:

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

// Conservative assumptions for maximum compatibility
const safeAssumptions: Assumptions = {
  constantSuper: false,
  noClassCalls: false,
  privateFieldsAsProperties: false,
  pureGetters: false
};

// Optimized assumptions for modern environments
const optimizedAssumptions: Assumptions = {
  constantSuper: true,
  noClassCalls: true,
  noNewArrows: true,
  privateFieldsAsProperties: true,
  pureGetters: true,
  setClassMethods: true,
  setPublicClassFields: true,
  arrayLikeIsIterable: true,
  iterableIsArray: true
};

// Using assumptions in SWC configuration
const jscConfig: JscConfig = {
  target: "es2020",
  assumptions: optimizedAssumptions,
  parser: {
    syntax: "typescript",
    tsx: true
  }
};

Array and Iteration Assumptions

Assumptions related to array-like objects and iteration behavior.

interface ArrayIterationAssumptions {
  /** Assume array-like objects implement Symbol.iterator */
  arrayLikeIsIterable?: boolean;
  /** Assume iterable objects are always arrays */
  iterableIsArray?: boolean;
  /** Skip cleanup when for-of loops terminate early */
  skipForOfIteratorClosing?: boolean;
}

Class and Object Assumptions

Assumptions about class behavior and object property handling.

interface ClassObjectAssumptions {
  /** Assume classes are never invoked without new */
  noClassCalls?: boolean;
  /** Assume arrow functions are never constructed */
  noNewArrows?: boolean;
  /** Transform private fields as regular properties */
  privateFieldsAsProperties?: boolean;
  /** Set class methods as enumerable properties */
  setClassMethods?: boolean;
  /** Set public class fields as enumerable */
  setPublicClassFields?: boolean;
  /** Set computed properties as enumerable */
  setComputedProperties?: boolean;
  /** Set spread properties as enumerable */
  setSpreadProperties?: boolean;
  /** Assume object rest doesn't include symbols */
  objectRestNoSymbols?: boolean;
}

Module and Function Assumptions

Assumptions about module behavior and function properties.

interface ModuleFunctionAssumptions {
  /** Assume re-exported bindings don't change */
  constantReexports?: boolean;
  /** Assume module metadata is enumerable */
  enumerableModuleMeta?: boolean;
  /** Skip incomplete namespace import detection */
  noIncompleteNsImportDetection?: boolean;
  /** Ignore function.length when transforming */
  ignoreFunctionLength?: boolean;
  /** Ignore function.name when transforming */
  ignoreFunctionName?: boolean;
}

Runtime Behavior Assumptions

Assumptions about runtime behavior and built-in operations.

interface RuntimeAssumptions {
  /** Assume super class constructor doesn't change */
  constantSuper?: boolean;
  /** Assume super is always a callable constructor */
  superIsCallableConstructor?: boolean;
  /** Assume getters have no side effects */
  pureGetters?: boolean;
  /** Assume document.all doesn't exist */
  noDocumentAll?: boolean;
  /** Ignore toPrimitive hint parameter */
  ignoreToPrimitiveHint?: boolean;
  /** Assume template objects can be mutated */
  mutableTemplateObject?: boolean;
}

Performance Impact

Enabling assumptions can significantly improve transformation performance and output size:

  • Memory Usage: Reduced by avoiding defensive checks
  • Bundle Size: Smaller due to simplified generated code
  • Runtime Performance: Faster execution with fewer indirections
  • Compatibility Risk: Higher chance of runtime errors if assumptions are violated

Migration from Babel

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

// Babel assumptions in .babelrc
{
  "assumptions": {
    "constantSuper": true,
    "noClassCalls": true
  }
}

// Equivalent SWC configuration
const swcConfig = {
  jsc: {
    assumptions: {
      constantSuper: true,
      noClassCalls: true
    }
  }
};

Install with Tessl CLI

npx tessl i tessl/npm-swc--types

docs

assumptions.md

ast-nodes.md

configuration.md

index.md

jsx.md

minification.md

modules.md

parser.md

typescript.md

tile.json