Skip to content
This repository has been archived by the owner on Dec 4, 2022. It is now read-only.

Context doesn't type null #13

Open
OmgImAlexis opened this issue Nov 22, 2022 · 2 comments
Open

Context doesn't type null #13

OmgImAlexis opened this issue Nov 22, 2022 · 2 comments
Labels
good first issue Good for newcomers

Comments

@OmgImAlexis
Copy link

import KingWorld from 'kingworld';

type Data = {
    field: number;
}

const app = new KingWorld()
    .state<'result', Data | null>('result', null)
    .get('/', context => context.store.result.field)
    .listen(3000);
	 
console.log(`🦊 KINGWORLD is running at :${app.server.port}`);

Context is typed as the following.

(parameter) context: Context<TypedSchemaToRoute<MergeSchema<{}, {}>>, {
    result: Data;
}>

result should be typed as Data | null.

Currently if I run this I get the following error when browsing to http://localhost:3000.

null is not an object (evaluating 'context.store.result.field')
@OmgImAlexis
Copy link
Author

undefined also don't seem to be typed correctly, it just gets removed like null.

@SaltyAom
Copy link
Owner

This seems to be the limitation of TypeScript, can you try set compilerOptions.strict to true and see if it now infers type correctly?

Here's a screenshot on my end, using strict: true results correct type.
Screenshot 2565-11-27 at 00 12 21

However when using strict: false, null is missing.
Screenshot 2565-11-27 at 00 12 36

Also you can use as instead of providing 2 generics to state, as it's intended for internal use.
Screenshot 2565-11-27 at 00 16 35

Let me know if this work for you~

@SaltyAom SaltyAom added the good first issue Good for newcomers label Nov 26, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants