Namespace not marked type-only declare

Viewed 231

I have a Typescript File that looks like this :

export namespace MyNamespace {
    export const myFunc = (a:number) => 3;
}

And when I compile it (in my case I am using storybook), I get this error :

Namespace not marked type-only declare. Non-declarative namespaces are only supported experimentally in Babel
. To enable and review caveats see: https://babeljs.io/docs/en/babel-plugin-transform-typescript

This is a babel trying to compile TS error as far as I can tell.

This error "Namespace not marked type-only declare" is puzzling because it doesn't seem to be proper english. Is there supposed to be a comma in there?

Am I supposed to declare something? What is a type only namespace?

1 Answers

declare is a keyword in Typescript, and "type-only" refers to its behavior. When you add the keyword declare to a namespace, it may only contain types. So, the error indicates that Babel doesn't (by default when the question was asked) support namespace unless you use that keyword, and you can only use the keyword if you only use types.

As for your example, it contains more than just types, so you need to go update to a more recent version (>= v7.13.0) of Babel that enables namespace support by-default, or change the configuration option to allowNamespaces: true as per the link in the error, https://babeljs.io/docs/en/babel-plugin-transform-typescript.

Related