Change Typescript Version

Run the following on a component that uses your env to get the typescript version currently being used:

$bit
Copiedcopy

The output is similar to the following:

teambit.compilation/compiler

configured compiler: typescript-compiler (TypeScript @ 4.9.4)

To provide your Typescript compiler with a different version of Typescript, implement the compiler env handler and replace your Typescript build task:

// @filename: my-env.bit-env.ts

/**
 * the base env
 */
import { ReactEnv } from '@teambit/react.react-env';
import type { ReactEnvInterface } from '@teambit/react.react-env';

import type { EnvHandler } from '@teambit/envs';
import {
  TypescriptTask
  TypescriptCompiler,
  resolveTypes,
} from '@teambit/typescript.typescript-compiler';

import typescript from 'typescript';

export class MyEnv extends ReactEnv implements ReactEnvInterface {

  constructor() {
    super();
  }

  name = 'my-env';

/**
 * replace the default compiler
 */
  compiler(): EnvHandler<Compiler> {
    return TypescriptCompiler.from({
      tsconfig: require.resolve('./config/tsconfig.json'),
      types: resolveTypes(__dirname, ['./types']),
      /**
       * use the typescript instance this env has as a dependency
       */
      typescript,
    });
  }

  build() {
    return super.build().replace([
      /**
       * replace the default typescript build task
       * with this instance of typescript
       */
      TypescriptTask.from({
        tsconfig: require.resolve('./config/tsconfig.json'),
        types: resolveTypes(__dirname, ['./types']),
        /**
         * provide the typescript version this env has as a dependency
         */
        typescript,
      }),
    ]);
  }
}

export default new MyEnv();
CopiedCopy

Install the version of typescript you want to use:

$bit
Copiedcopy
See command synopsis

Typescript is automatically detected as a dependency of that env. By default, dependencies are resolved to the ones installed in that workspace.

Run the following to compile your env:

$bit
Copiedcopy
See command synopsis