Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TS] Using type only imports to use types "normally"? #40

Open
MarcusRiemer opened this issue Aug 11, 2021 · 5 comments
Open

[TS] Using type only imports to use types "normally"? #40

MarcusRiemer opened this issue Aug 11, 2021 · 5 comments

Comments

@MarcusRiemer
Copy link

The typescript setup guide tells the user to add "typeRoots": ["./node_modules"] to the tsconfig.json. This seems a little dangerous to me and from my PoV this could also be solved by using Type-Only imports from Typescript 3.8.
I went ahead and changed declare namespace nkruntime (see https://github.com/heroiclabs/nakama-common/blob/master/index.d.ts#L15) to export namespace nkruntime and then a corresponding import { nkruntime } from "nakama-runtime"; in my typescript file.

For me this works fine and nicely streamlines the development to be less "unusual".

@novabyte
Copy link
Member

@MarcusRiemer Happy to take on this advice. Can you explain what you mean by dangerous about adding to the typeRoots? I'm not a TypeScript export so any help is appreciated.

@MarcusRiemer
Copy link
Author

"Dangerous" in the sense of "Marcus was once bitten by a globally declared conflicting var Observable: any that some unrelated package provided and therefor forgot to import RxJS after copy and pasting stuff around which led to surprising errors at runtime".

If I understand correctly the nkruntime functions are exposed to Typescript but are implemented in Go? So they are guaranteed to be available, even though there is no visible implementation? In that case the type only imports should perfectly fit this usecase, as they are purely compile time constructs and therefore never surface in the compiled JavaScript.

@MarcusRiemer
Copy link
Author

fwiw: I ran through most of the Typescript setup process (including Rollup, excluding building via docker) with the import type { nkruntime } from "nakama-runtime"; and the hacked index.d.ts and everything seems to work fine. I was a little puzzled that everything also works with the normal import statement which is probably due to the fact that all callbacks receive the relevant instances as arguments anyway. So I guess its possible to just use a normal import, but I will see what happens if I try to do some more interesting things.

@orta
Copy link

orta commented Dec 25, 2021

What I did was add:

/// <reference types="nakama-runtime" />

To the top of my index.ts file for the server, which brought in the imports without a tsconfig change.

I debated adding export = nkruntime at the end of the .d.ts in this file and then submitting those changes here which would allow for import type nkruntime from "nakama-runtime". But if all of the globals are always in the runtime, then it felt like allowing for the import is a bit unrealistic and left it at that.

@bananu7
Copy link
Contributor

bananu7 commented Jan 27, 2022

That's also how I solved it (with the old style reference)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants