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

JSON property value type #266

Open
rpidburachynskyi opened this issue Nov 2, 2021 · 9 comments
Open

JSON property value type #266

rpidburachynskyi opened this issue Nov 2, 2021 · 9 comments
Labels
enhancement New feature or request v10.1

Comments

@rpidburachynskyi
Copy link

rpidburachynskyi commented Nov 2, 2021

Library contains good type Primitive, its very useful, it's good idea to have the same type but for types from JSON, something like that: type JSONFieldType = string | number | boolean | null | Record<string, JSONFieldType>; (but with better name)

@Beraliv Beraliv added the enhancement New feature or request label Nov 4, 2021
@Beraliv
Copy link
Collaborator

Beraliv commented Nov 4, 2021

@kitsoRik thank you again for the good suggestion!

At the moment I'm preparing for the conference and will be back next week!

For me the plan sounds good, I really like the idea and most probably we will start adding it soon

cc @krzkaczor

@rpidburachynskyi
Copy link
Author

@Beraliv Yeah, this is a good package that I install in all projects where I use TypeScript, and I would like to have there all common types. That's not a problem for me (about your busy status). Anyway, I need this type in my current project and I just put it in my types folder.

FYI, now it looks like this:

export type JsonFieldType =
  | string
  | number
  | boolean
  | null
  | { [path: string]: JsonFieldType }
  | JsonFieldType[];

I'm not sure this is finished implementation, so you need to test all cases.

@rpidburachynskyi
Copy link
Author

rpidburachynskyi commented Nov 26, 2021

Hi. Regarding my implementation above, found a strange behaviour with inherited classes.

export type JsonFieldType =
  | string
  | number
  | boolean
//   | undefined // uncomment to fix
  | null
  | { [path: string]: JsonFieldType }
  | JsonFieldType[];

abstract class A {
    method(): JsonFieldType {
        if(Math.random() > 0.5) {
            return { q: 1 };
        }

        return { q: 1 };
    }
}

class B extends A {
    method()/*: JsonFieldType */ { // uncomment to fix
        if(Math.random() > 0.5) {
            return { q: 1 };
        }

        return { w: 1 };
    }
}

Probably - good way to add undefined to the union, or no, IDK.

@Beraliv
Copy link
Collaborator

Beraliv commented Nov 26, 2021

Hmm, that's weird

I tried it here – https://tsplay.dev/WPx85W and doesn't have any issues

What TypeScript do you have?

@rpidburachynskyi
Copy link
Author

Hmm, that's weird

I tried it here – https://tsplay.dev/WPx85W and doesn't have any issues

What TypeScript do you have?

Yeah, because you uncommented some strings. It works fine without undefined (only with implicitly defined return type of method)

@Beraliv
Copy link
Collaborator

Beraliv commented Nov 26, 2021

Hmm, that's weird
I tried it here – https://tsplay.dev/WPx85W and doesn't have any issues
What TypeScript do you have?

Yeah, because you uncommented some strings. It works fine without undefined (only with implicitly defined return type of method)

Ah, sorry, I needed to have it without uncommenting

@Beraliv
Copy link
Collaborator

Beraliv commented Dec 19, 2021

Also this can be connected with the future implementation of JsonFieldType#276

@Beraliv
Copy link
Collaborator

Beraliv commented Dec 19, 2021

Also found interesting implementation here – microsoft/TypeScript#1897 (comment)

@Beraliv Beraliv changed the title Primitive but only for JSON JSON property value type Mar 16, 2023
@Beraliv Beraliv added v10 v10.1 and removed v10 labels Apr 28, 2024
@Beraliv
Copy link
Collaborator

Beraliv commented May 1, 2024

Hey @rpidburachynskyi! I'm planning to include it in the next minor change, would you be interested in having this type now?

I'm planning to implement JsonPrimitive, JsonObject, JsonArray and JsonValue based on https://www.rfc-editor.org/rfc/rfc8259#section-3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request v10.1
Projects
None yet
Development

No branches or pull requests

2 participants