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

fix: more reliable typing of JSONObject #4876

Closed
wants to merge 2 commits into from
Closed

Conversation

GauBen
Copy link
Contributor

@GauBen GauBen commented May 11, 2022

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpx changeset and following the prompts. All changesets should be patch until SvelteKit 1.0

Before:
image

It ends with:

 Type 'Stuff' is not assignable to type 'JSONObject'.
    Index signature for type 'string' is missing in type 'Stuff'

After:
image

I have no idea what's the difference, but Record works.

I don't plan to investigate any further.


type Record<K extends keyof any, T> = {
    [P in K]: T;
};

@changeset-bot
Copy link

changeset-bot bot commented May 11, 2022

🦋 Changeset detected

Latest commit: 4d92cf3

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@Rich-Harris
Copy link
Member

This is failing CI because this...

export type JSONObject = Record<string, JSONValue>;

...introduces a circular dependency between JSONObject and JSONValue. I don't know why that doesn't happen with interfaces or how to fix it.

@benmccann benmccann requested a review from ignatiusmb May 12, 2022 03:52
@ignatiusmb
Copy link
Member

Although it used to be about type or interfaces, that was now fixed. The difference here lies in how we define them, one with the a generics and the other directly as an object literal. A type alias can't be referenced by itself at the top level, and the trick with generics is that they will unpack/expand anything that is passed upon them, which means it won't count as 'literals' even though we're abstracting that object literal in another type — what the Record implementation is.

Which also means it won't satisfy this rule (from the archived spec)

Note that object type literals, function type literals, and constructor type literals do not depend on types referenced within them and are therefore permitted to circularly reference themselves through type aliases.

@Rich-Harris
Copy link
Member

So is there any way to make this work?

@ignatiusmb
Copy link
Member

I suppose, yeah?

@ignatiusmb ignatiusmb mentioned this pull request May 13, 2022
5 tasks
@Rich-Harris
Copy link
Member

closing in favour of #4897

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants