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

Type Mismatch in @remix-run/node Library #10231

Open
Garajluc opened this issue Nov 15, 2024 · 0 comments
Open

Type Mismatch in @remix-run/node Library #10231

Garajluc opened this issue Nov 15, 2024 · 0 comments

Comments

@Garajluc
Copy link

Garajluc commented Nov 15, 2024

Reproduction

After upgrading @remix-run/node from version ^2.7.0 to ^2.14.0, I encountered a type mismatch issue when using createCookieSessionStorage with a custom session data type. The type mismatch occurs because the createCookieSessionStorage function expects a generic type that extends SessionData, but it does not allow restricting the generic to specific keys.

Steps to Reproduce

  1. Define a custom session data type with specific keys:
interface CustomSessionData {
  customProperty?: boolean;
}
  1. Create a session storage using createCookieSessionStorage with the custom session data type:
import { createCookieSessionStorage } from "@remix-run/node";

export const sessionStorage = createCookieSessionStorage<CustomSessionData>({
  cookie: {...},
});
  1. Use the session storage in an authenticator:
import { Authenticator } from "remix-auth";
import { sessionStorage } from "./session";

export const authenticator = new Authenticator<Auth>(sessionStorage, {
  sessionKey,
});
  1. The sessionStorage usage in the step 3, yells the typescript error pasted in the "Actual Behavior" section

System Info

System:
    OS: macOS 14.1
    CPU: (11) arm64 Apple M3 Pro
    Memory: 125.41 MB / 18.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.11.0 - ~/.nvm/versions/node/v22.11.0/bin/node
    Yarn: 1.22.22 - ~/.nvm/versions/node/v22.11.0/bin/yarn
    npm: 10.9.0 - ~/.nvm/versions/node/v22.11.0/bin/npm
  Browsers:
    Chrome: 130.0.6723.119
    Safari: 17.1
  npmPackages:
    @remix-run/dev: ^2.7.0 => 2.7.2 
    @remix-run/eslint-config: ^2.6.0 => 2.6.0 
    @remix-run/node: ^2.7.0 => 2.7.2 
    @remix-run/react: ^2.7.0 => 2.12.1 
    @remix-run/serve: ^2.7.0 => 2.7.2 
    @remix-run/testing: ^2.12.1 => 2.12.1 
    vite: ^5.4.8 => 5.4.8

Used Package Manager

npm

Expected Behavior

The custom session data type should be compatible with the SessionData interface, and there should be no type mismatch.

Actual Behavior

A type mismatch error occurs:

Argument of type 'SessionStorage<{ customProperty?: boolean | undefined; }, { customProperty?: boolean | undefined; }>' is not assignable to parameter of type 'SessionStorage<SessionData, SessionData>'.
  The types returned by 'getSession(...)' are incompatible between these types.
    Type 'Promise<Session<{ customProperty?: boolean | undefined; }, { customProperty?: boolean | undefined; }>>' is not assignable to type 'Promise<Session<SessionData, SessionData>>'.
      Type 'Session<{ customProperty?: boolean | undefined; }, { customProperty?: boolean | undefined; }>' is not assignable to type 'Session<SessionData, SessionData>'.
        Types of property 'get' are incompatible.
          Type '<Key extends "customProperty" >(name: Key) => (Key extends "customProperty" ? { customProperty?: boolean | undefined; }[Key] : undefined) | (Key extends "customProperty" ? { ...; ...' is not assignable to type '<Key extends string>(name: Key) => (Key extends string | number ? any : undefined) | (Key extends string | number ? any : undefined) | undefined'.
            Types of parameters 'name' and 'name' are incompatible.
              Type 'Key' is not assignable to type '"customProperty"'.
                Type 'string' is not assignable to type '"customProperty"'.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants