sveltejs / sveltejs/kit

Allow `App.PageState` to be a union type

Open
#11,719 3 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

types / typescript
Dominant language
JavaScript
Stars
20.8k
Forks
2.3k
Avg merge
1d 16h
Merged PRs (30d)
156

Description

Describe the problem

I want to have typechecked history state, so after some googling, I found I could do it with App.PageState like this:

declare global {
  namespace App {
    export interface PageState {
      // Declare properties here
    }
  }
}

export {};

However, I am using the page state to open modals for e.g. editing stuff, so I want to sometimes save some data in the state. Ideally, I would like to be able to use something like a union to represent this

Describe the proposed solution
declare global {
  namespace App {
    export type PageState =
      | {
           type: "listFoo";
        }
      | {
            type: "addFoo";
            fooID: string;
        };
  }
}

export {};

But this doesn't work

Alternatives considered
type Temp =
  | {
       type: "listFoo";
    }
  | {
        type: "addFoo";
        fooID: string;
    };

declare global {
  namespace App {
    export interface PageState extends Temp {}
  }
}

export {};

This doesn't work either...

Importance

nice to have

Additional Information

No response

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 locating the declaration and related type-checking coverage for App.PageState, then trace how the global type is consumed. Done means a discriminated union can be declared for PageState without breaking existing state typing or related checks.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.