Skip to content

Work with isolatedModules

Jiuqing Song edited this page May 4, 2022 · 1 revision

When use roosterjs in a projection that is compiled with option "isolatedModules", you may hit the error like:

import * as RoosterJs from 'roosterjs';

let div: HTMLDivElement = document.createElement('div');
const editor = new RoosterJs.Editor(div);
RoosterJs.setAlignment(editor, RoosterJs.Alignment.Center);
editor.ts:5:32 - error TS2748: Cannot access ambient const enums when the '--isolatedModules' flag is provided.

5 RoosterJs.setAlignment(editor, RoosterJs.Alignment.Center);
                                 ~~~~~~~~~~~~~~~~~~~
Found 1 error.

This is because the types you are using such as RoosterJs.Alignment is exported as const enum, which is not compatible with isolatedModules option.

Start from version 8.22.0, we have added a new package to export enum types that have the same values with those const enum types, and have prefix "Compatible" in their names. For example:

/**
 * enum for setting block alignment, used by setAlignment API
 */
export declare enum CompatibleAlignment {
    /**
     * Align left
     */
    Left = 0,
    /**
     * Align center
     */
    Center = 1,
    /**
     * Align right
     */
    Right = 2
}

These types are exported from a new package roosterjs-editor-types-compatible. So you can import them from either this new package or roosterjs package. All the public API that uses const enum are now also accepting these compatible types.

import * as RoosterJs from 'roosterjs';

let div: HTMLDivElement = document.createElement('div');
const editor = new RoosterJs.Editor(div);
RoosterJs.setAlignment(editor, RoosterJs.CompatibleAlignment.Center);

For a full list of new compatible types, please see here

Clone this wiki locally