microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.94k stars 12.47k forks source link

Support import of jsonc file to infer type #54137

Open gkhub opened 1 year ago

gkhub commented 1 year ago

Suggestion

🔍 Search Terms

typescript jsonc

json jsonc

✅ Viability Checklist

My suggestion meets these guidelines:

⭐ Suggestion

Enable supporting import of jsonc file to provide typing information.

import * as data from './test.jsonc';

type myType = typeof data;

The import statement for jsonc is not supported today.

📃 Motivating Example

import * as data from './test.jsonc';

type myType = typeof data;

💻 Use Cases

Our scenario auto generates UX based on declarative jsonc file. We have a requirement to load and infer the type specified in the jsonc file to invoke some component in the UX. There is no typing support currently and workaround is to manually construct the data.

gerardo-lima-moonfare commented 1 year ago

Unfortunately -- and ironically -- import { compilerOptions } from 'tsconfig.json'; doesn't usually work. It would be great to be able to import jsonc files as easily as importing regular json files.

zsolt-dev commented 11 months ago

Importing json files with comments should be supported. Comments are much needed in the configuration files which are mostly json. I know that the json spec prohibits comments, but everyone is eventually pushed to support comments, which would mean that the spec should change too. Even tsconfig.json supports comments, but like @gerardo-lima-moonfare mentioned, you cannot import it :)

jwueller commented 8 months ago

I might be wrong here, but I seem to remember that the JSON spec allows a parser to accept comments, but not to emit them when serializing. So it would likely be compliant to just allow importing them directly too.

pavinduLakshan commented 4 months ago

Is there any update on this 👀 ?

marek-vodvarka commented 6 days ago

+1 for this, would be really useful