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.

Please provide a `json` basic type

See original GitHub issue

Hi,

JSON is an important data exchange format. At present there is no explicit way to annotate an object as pure JSON. We were expecting union types to solve this problem via the following:

    interface Json {
        [x: string]: string|number|boolean|Date|Json|JsonArray;
    }
    interface JsonArray extends Array<string|number|boolean|Date|Json|JsonArray> { }

There are currently a number of problems with this.

  • Contextual typing is absent in parenthetic expression #920

      interface Id extends Json {
          id: number;
      }
    
      var z = (): Id => ({id: 'foo'}); // Error: Missing index signature
    
  • Contextual typing does not flow from base to subclasses #1373

class Bass {

    f(): Id{ return undefined;}
}

 // Error: missing index signature
class Foo extends Bass {
    f() { return { id: 10 }} 
}
  • Object literals require explicit index signatures #1889:
    interface Foo extends Json {

        foo: { val: string }; // Error: Not assignable

    }
  • Other problems:

    // Error: Missing index signature
    var result: Id[] = 'a|b'.split('|').map(item => {
          return { id: 0 };
    });
    

The first two problems look likely to be resolved, but not the last two.

This motivates the introduction of a natively supported json type that conforms to the JSON spec.

It should be possible to derive a custom JSON object from the json type with non of the problems above. Furthermore, since JSON is a data exchange format it should be possible to mark fields as nullable on types deriving from json (which would trigger a type guard).

I am of course aware that this is all rather vague!

Issue Analytics

  • State:open
  • Created 9 years ago
  • Reactions:399
  • Comments:75 (6 by maintainers)

github_iconTop GitHub Comments

99reactions
AlicanCcommented, Jun 22, 2015

Since when JSONs are objects?

type Json = string;
84reactions
AlicanCcommented, Jun 23, 2015

Yes, the JSON becomes an object and it’s not JSON anymore. JSON is what you give to JSON.parse(), not what you get from it. What you get is a value. Maybe you guys should rename your types to JsonValue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

JSON | Data Types - GeeksforGeeks
JSON supports mainly 6 data types: string; number; boolean; null; object; array. Note: string, number, boolean, null are simple data types ...
Read more >
JSON - Quick Guide - Tutorialspoint
JSON is easy to read and write. It is a lightweight text-based interchange format. JSON is language independent. Simple Example in JSON. The...
Read more >
JSON Tutorial: Learn with Simple File Format EXAMPLE
JSON Tutorial for beginners: JSON stands for JavaScript Object Notation, JSON is a file format used to store information in an organized and ......
Read more >
Learn the Working of TypeScript JSON type - eduCBA
Let us see some Example for JSON Type in TypeScript:- Let us make JSON Data in TypeScript with the ... The output for...
Read more >
JSON Basics: What You Need to Know
Nearly all programming languages contain functions or libraries that can read and write JSON structures. JSON stands for JavaScript Object ...
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 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