olivierr91
olivierr91

Reputation: 1415

How to import enum in interface declaration file (d.ts)

I want to extend string with a method that needs an enumeration to be passed. How do I import that enum into the declaration file?

CapitalizationStyle.tsx:

export enum CapitalizationStyle {
    None = 0,
    Lowercase = 1,
    Word = 2
}

StringExtensions.d.ts:

import { CapitalizationStyle } from "Utils/CapitalizationStyle"; //This line breaks everything.

declare interface String {
    applyCapitalizationStyle(this: string, style: CapitalizationStyle): string;
}

The import breaks the interface declaration, like if the declaration does not exist anymore. All extension implementations of the String class become invalid as soon as I add the import:

StringExtensions.tsx:

enter image description here

Minimal reproducible example project: https://wetransfer.com/downloads/d1a707c0ac734985b877058967c35a6820171212143715/410f48

Upvotes: 3

Views: 1218

Answers (2)

gsamaras
gsamaras

Reputation: 73384

Try wrapping in curly braces, like this:

import {CapitalizationStyle} from "Utils/CapitalizationStyle";

since you don't default-export your custom enumeration.

Upvotes: 0

msanford
msanford

Reputation: 12237

Since you don't have a default export from that module, you need to wrap it in {}:

import { CapitalizationStyle } from "Utils/CapitalizationStyle";

Upvotes: 2

Related Questions