1

I have setup an ASP.net core MVC project with TS. I want to use TS without using any module system. Just "clean" compiled TS=>JS.

The problem I am facing, is that I can't reference a declaration file (.d.ts) in a .ts file, without using "import".

If I do use "import" code completion and type checking works, but because I have configured my project not to use any "module" system, compiler complains saying: "Cannot use imports, exports or module augmentations when '--module' is 'none'

I've already tried using triple slash directives Triple-Slash Directives and using @types, typeRoots and types in tsconfig.json files but no luck.

VS2017(same with VS Code) doesn't seem to be able resolve the referenced file.

I've set up a rudimentary project which demonstrates the problem https://github.com/gtrifidis/Asp.netCoreTypescriptDemo/tree/master/TypeScriptDemo

Any help/idea would be very much appreciated.

1 Answer 1

1
+50

The problem is not that TypeScript doesn't find your d.ts file, but because in your case the file index.d.ts of i18next is written in the module fashion, and is supposed to be used only with import. Luckily, it is very easy to fix this; all you have to do is to go to index.d.ts and remove the last line:

....
declare const i18next: i18next.i18n;
export = i18next; // <- remove this line

And then you can go ahead and remove the import in your LocalizationTest.ts, and it will compile just fine.

Update

Or, as noted by @AluanHaddad, instead of removing it and create your own definition, you should submit a PR that adds a new line:

....
declare const i18next: i18next.i18n;
export = i18next;
export as namespace i18next; // <- add this instead
Sign up to request clarification or add additional context in comments.

2 Comments

This is correct, but there are two things that should be noted. 1: the npm package now ships with its own type definitions (which are still wrong in the manner you described). 2: There is a UMD bundle distributed as part of that package, allowing you to use it as a global, but the type declarations do not reflect that. Therefore, the correct fix is to submit a PR that uses the UMD style declaration syntax so, instead of removing the export clause, add export as namespace i18next;
@AluanHaddad and Mu-Tsun-Tsai thank you for your help. Here is the pull request PR just FYI in case you were interested.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.