microsoft / microsoft/TypeScript

Refine JSON.stringify return type for booean value

Open
#38,160 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Search Terms

JSON.stringify "true" "false"

Suggestion

I'd like to have additional typings for the return type of JSON.stringify, namely when given a boolean, the return type should be 'true' | 'false'

Use Cases

In React assigning the ARIA boolean attributes requires a boolean-string, currently you have to do:

<div aria-expanded={isExpanded ? 'true' : 'false'}/>

With the change you could simplify that to:

<div aria-expanded={JSON.stringify(isExpanded)}/>

Examples

const r1: 'true' | 'false' = JSON.stringify(true);
// Or if we could have it be even more granular:
const r2a: 'true' = JSON.stringify(true);
const r2b: 'false' = JSON.stringify(false);
``

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

No file or test path is named in the issue. Start by locating the TypeScript declaration and existing tests for JSON.stringify, then compare boolean inputs with the requested literal return types. Done means the boolean examples type-check with the intended return type while existing stringify behavior remains covered.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.