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

Allow checking json in conjunction with .d.json.ts files #52994

Open
5 tasks done
Jamesernator opened this issue Feb 27, 2023 · 2 comments
Open
5 tasks done

Allow checking json in conjunction with .d.json.ts files #52994

Jamesernator opened this issue Feb 27, 2023 · 2 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@Jamesernator
Copy link

Jamesernator commented Feb 27, 2023

Suggestion

๐Ÿ” Search Terms

checkJson, check json, .d.json.ts

โœ… Viability 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. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

โญ Suggestion

So in TS 5.0 we can add .d.json.ts files to type JSON files, i.e.:

// config.d.json.ts

declare const config: {
    option?: string,
    option2?: number,
};

export default config;
// config.json
{
    "option": "foo",
    "option2": 12
}
// main.ts
import config from "./config.json" assert { type: "json" };

// config has type { option?: string, option2?: number }

This is nice, but it would be better if we could validate that the corresponding .json files do in fact actually match the type. I'd like to propose a new option, checkJson that when set validates types errors within JSON files.

For example suppose we have:

// config.d.json.ts
declare const config: {
    value: string,
};

export default config;
// config.json
{
    "value": 3
}

Then TypeScript would report a type error when running tsc on the project (or in the editor).

๐Ÿ“ƒ Motivating Example

Pretty much anywhere manual JSON files are used in conjunction with .d.json.ts files would be useful. The point of TS is to help detect errors, extending this to JSON is a natural extension.

In particular this would make a particularly good alternative to JSON schema support as I proposed in another issue.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Feb 27, 2023
@fatcerberus
Copy link

This potentially opens a larger can of worms - should .js files be type-checked in accordance with their corresponding .d.ts as well? Currently this doesnโ€™t happen, by design, though several people have asked for it (and been declined).

@Jamesernator
Copy link
Author

This potentially opens a larger can of worms - should .js files be type-checked in accordance with their corresponding .d.ts as well? Currently this doesnโ€™t happen, by design, though several people have asked for it (and been declined).

With JS people can already use jsdoc, with JSON there is simply nowhere we could put TS types (perhaps in JSONC, but browsers/node/deno/etc don't support importing that), I did propose using JSON Schema but there seemed to be no interest due to how different it is from TS types.

spalladino added a commit to AztecProtocol/aztec-packages that referenced this issue Jun 27, 2024
Potential fix for Typescript LSP being slow.

Based on
microsoft/TypeScript#42761 (comment)
and microsoft/TypeScript#52994

---------

Co-authored-by: Santiago Palladino <santiago@aztecprotocol.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants