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

Add generic type for session #1791

Merged
merged 2 commits into from
Jul 1, 2021
Merged

Add generic type for session #1791

merged 2 commits into from
Jul 1, 2021

Conversation

seanlail
Copy link
Contributor

@seanlail seanlail commented Jul 1, 2021

Previously, in Sapper, we could use generics to ensure our session type was used correctly:

E.g. if I had a session type that was { foo: string; }

import { stores } from "@sapper/app";

const { session } = stores<MySessionType>();

$session.foo; // correct usage
$session.bar; // invalid, compiler shows error

This PR adds the same generic support in getStores as well as a 3rd param in the generic for LoadInput

SvelteKit:

import { getStores } from "$app/stores";

const { session } = getStores<MySessionType>();

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

@changeset-bot
Copy link

changeset-bot bot commented Jul 1, 2021

🦋 Changeset detected

Latest commit: 0548e43

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

@seanlail
Copy link
Contributor Author

seanlail commented Jul 1, 2021

@dummdidumm GrygrFlzr pointed out on Discord that this PR doesn't help with this:

import { session } from '$app/stores';

I wonder if there's anything we could do here?
I had a quick search and found this https://stackoverflow.com/questions/42986950/how-to-define-import-variable-type

i.e. update your own ambient type per project, something like this:

declare module "$app/stores" {
    const session : MySessionType;
    export = session;
}

If that's the only solution, perhaps it should be documented?

@dummdidumm
Copy link
Member

That seems like a useful addition, but I wouldn't add it to the site docs, only to the jsdoc in the ambient-modules.d.ts file

@seanlail
Copy link
Contributor Author

seanlail commented Jul 1, 2021

Odd. I just tested this in the example project.

In a component (with a typo: fo instead of foo)

<script lang="ts">
  import { session } from "$app/stores";
</script>

<div>{$session.fo}</div>

This doesn't work, VSCode and build / lint scripts think session in the component is Writable<any>

/// <reference types="@sveltejs/kit" />

declare module "$app/stores" {
  import type { Writable } from "svelte/store";

  export const session: Writable<{ foo: string; bar: string }>;
}

However, if I remove the reference it all works and VSCode picks up the error.

Does this line actually need to be included?

/// <reference types="@sveltejs/kit" />

@dummdidumm
Copy link
Member

Yes it needs to be included, else other imports from $.. and .svelte imports in TS components are no longer recognized by TypeScript (giving false positives). My guess is that the broader type wins here, since both definitions are loaded, so this approach won't work. If people really want session to be a certain writable, I guess they have to do something like export const myTypedSession: Writable<SomeInterface> = session. But I wouldn't add this to the docs then.

@dummdidumm dummdidumm merged commit 0d69e55 into sveltejs:master Jul 1, 2021
dummdidumm pushed a commit that referenced this pull request Jul 2, 2021
@janosh
Copy link
Contributor

janosh commented Aug 13, 2021

I'm prob missing something. Why doesn't SvelteKit propagate the return type of getSession from src/hooks.ts?

I have

// src/hooks.ts

export function getSession(): Record<string, string> {
  // ...
  return session
}

yet when I import

import { session } from '$app/stores'

it has type

(alias) const session: Writable<any>

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.

None yet

3 participants