question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

json files should be usable in .d.ts files without breaking things

See original GitHub issue

Search Terms

resolveJsonModule import definition

Suggestion

As a .json file is a static resource, it should be able to be referenced in a global.d.ts file for example without that file being automatically treated as a module. The reason for this is that a .json file may be used to simply enforce valid translation keys, and you shouldn’t need to suck the whole .json file into the transpilation process to do that.

Use Cases

Example:

import translations from "../assets/translations/en-AU.json"
type $translate = {
    instant: (translationKey: keyof typeof translations) => string,
    use: Function,
    proposedLanguage: Function
}

The downside of the current method again is that the translations file is output in the transpilation process, which is wasteful if the file is large.

What the above code snippet is trying to achieve, should be allowed in a .d.ts file without breaking global declaration functionality.

Checklist

My suggestion meets these guidelines:

  • This wouldn’t be a breaking change in existing TypeScript / JavaScript code
  • This wouldn’t change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn’t a runtime feature (e.g. new expression-level syntax)

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:5
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
AlCalzonecommented, Nov 8, 2018

I just ran into the same issue. Given the following directory structure:

build/  - The compiler output ends up here
src/    - The sources reside here
  file1.ts
  file2.ts
  ... and so on
somejsonfile.json - The file I'm trying to include. It is guaranteed to be there in the final module

Compiling the source code results in this structure if I don’t reference somejsonfile.json from source code:

build/  - The compiler output ends up here
  file1.js
  file1.d.ts
  ... and so on
src/    - The sources reside here
  file1.ts
  file2.ts
  ... and so on
somejsonfile.json - The file I'm trying to include. It is guaranteed to be there in the final module

If I do import from somejsonfile.json, instead of just compiling from src to build, the structure is shifted:

build/  - The compiler output ends up here
  src/  - This is extraneous!
    file1.js
    file1.d.ts
    ... and so on
  somejsonfile.json  - This is just copied from the root dir.
src/    - The sources reside here
  file1.ts
  file2.ts
  ... and so on
somejsonfile.json

which results in duplication of the json file and an unnecessary src directory inside build.

Unfortunately, this means that resolveJsonModule is unusable for what I’m trying to do - that is reading strongly typed config data from a json file.

@mhegazy Is this the kind of feedback you’re looking for?

2reactions
noeleliascommented, Aug 24, 2019

How about another parameter. “compileJsonModule”:true|false Current code would still work and for future implementations devs would have the option to keep the strucutre.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do you produce a .d.ts "typings" definition file from an ...
from what I can tell, tsc can compile any valid JavaScript, but if it is not valid TypeScript (compile gives warnings) then the...
Read more >
Highcharts TypeScript Declarations (beta)
They can be used in production, but we can't guarantee your code from breaking with future type updates. In that case, probably small ......
Read more >
TypeScript rules for Bazel - bazelbuild/rules_nodejs
The ts_project rule invokes the TypeScript compiler on one compilation unit, or "library" (generally one directory of source files). In TypeScript terms, this ......
Read more >
Configuring a .d.ts rollup - API Extractor
ts rollup generation, you simply need to set dtsRollup.enabled to true in your api-extractor.json config file. By default, the rollup file will be...
Read more >
rollup.js
exports , you should change the file extension to .cjs . You can also use other languages for your configuration files like TypeScript....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found