microsoft / microsoft/TypeScript

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

Open
#52,994 2 comments 8 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing how TypeScript handles .d.json.ts declarations and imports of config.json when running tsc or in the editor. Define the behavior of the proposed checkJson option using the examples in the issue, including reporting a type error when JSON values do not match the declaration. Done means corresponding JSON files are validated without changing emitted JavaScript.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.