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

The typing of class SceneContextScene is incorrect #1827

Open
Evertt opened this issue Apr 9, 2023 · 1 comment · May be fixed by #1828
Open

The typing of class SceneContextScene is incorrect #1827

Evertt opened this issue Apr 9, 2023 · 1 comment · May be fixed by #1828

Comments

@Evertt
Copy link

Evertt commented Apr 9, 2023

Context

So I have this:

type SceneSessionData = {
    current?: string
    expires?: number
    state?: {
        lastMessageId?: number
    }
}

type MyContext = Context & {
    session: {
        __scenes?: SceneSessionData
    }
    scene: Scenes.SceneContextScene<MyContext, SceneSessionData>
}

And then looking at the implementation of the class SceneContextScene I see this code:

    get state() {
        return (this.session.state ??= {})
    }

    set state(value) {
        this.session.state = { ...value }
    }

But then the type definition of the class in typings/scenes/context.d.ts shows this:

export default class SceneContextScene<C extends SessionContext<SceneSession<D>>, D extends SceneSessionData = SceneSessionData> {
	// ...
    get session(): D;
    get state(): object;
    set state(value: object);
    // ...
}

Which means that when I do const state = ctx.scene.state, I'd expect state to be of type { lastMessageId?: number }, because that's what it actually is, but because of the type definition state is given with type object.

Solution

Change src/scenes/context.ts to this:

export default class SceneContextScene<C extends SessionContext<SceneSession<D>>, D extends SceneSessionData = SceneSessionData> {
    // ...
    get session(): D;
    get state(): D["state"];
    set state(value: D["state"]);
    // ...
}

And then I assume typings/scenes/context.d.ts will automatically be generated correctly.

@Evertt Evertt linked a pull request Apr 9, 2023 that will close this issue
@MKRhere
Copy link
Member

MKRhere commented Sep 2, 2023

Duplicate of #894?

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

Successfully merging a pull request may close this issue.

2 participants