Type safety: `Context.state` is unsound
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 34/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- deno, typescript
- Domain
- backend, developer-experience
Research direction
Start in packages/fresh/src/context.ts at the state initialization linked in the issue, then trace how App creates contexts and types Context.state. Compare the proposed initialization options against the example in main.ts and its State definition in utils.ts. Done means the state API has a sound type and its initialization behavior is covered by relevant tests.
Written by the indexing model from the issue text.
Description
I'm exploring fresh 2 via deno run -Ar jsr:@fresh/init.
In the example app, State and define are exported from utils.ts as follows:
import { createDefine } from "fresh";
// This specifies the type of "ctx.state" which is used to share
// data among middlewares, layouts and routes.
export interface State {
shared: string;
}
export const define = createDefine<State>();
My first instinct on reading this was that something must be required to set a default value for State when constructing the app, otherwise there's no way to ensure it will match its type.
Looking inside main.ts we find:
import { App, staticFiles } from "fresh";
import { define, type State } from "./utils.ts";
export const app = new App<State>();
app.use(staticFiles());
// Pass a shared value from a middleware
app.use(async (ctx) => {
ctx.state.shared = "hello";
return await ctx.next();
});
// <snip>
So, we're able to instantiate an application without passing a default State value. The default middleware described as "pass a shared value" is in fact doing the job of initialising the state, without it the state's value is simply {}. If ctx.state.shared = "hello" is commented out, TypeScript remains happy while the runtime value of the field is undefined.
Looking into the code, the culprit is here:
This is unsound unless {} is actually assignable to State (e.g. all State's fields are optional), the as just makes TypeScript ignore the issue.
Depending on the project's view on type safety this might be acceptable, e.g. it's up to the framework user to either supply a state type for which {} is a valid value, or to ensure that nothing depends on it before it's properly initialised (as is the case for the init project).
Otherwise, I can imagine three alternatives that would be sound:
-
Require initialisation, e.g.
new Context(...)should also take a value forstate. This could be integrated by, e.g.,new App(...)having an extra argumentdefaultState: Statewhich is copied into newContexts (ordefaultState: () => Statewhich is called for each request). -
Make the state type
Partial<State>, e.g.Context.state: Partial<State> = {}. This is type safe (no cast needed) but forces all uses of the state to deal with the possibility of fields being uninitialised, even if the application logic guarantees they would be initialised by that point. -
Make the uninitialised state explicit, e.g.
Context.state: State | {} = {}orContext.state: State | "uninitialized" = "uninitialized". As above, this is type safe but forces all uses to deal with uninitialisation.
To me, the best option would seem to be the first. That would allow framework users to provide a complete default if possible, and if not possible they can account for it in whichever way they prefer:
- They can achieve the current behaviour by using
{} as Stateas the default. - They can use an initialisation scheme that makes sense for each field of the state (e.g. default value, optional fields, or explicit uninitialised states).
- Dominant language
- TypeScript
- Stars
- 13.8k
- Forks
- 757
- Avg merge
- 3m
- Merged PRs (30d)
- 2
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from freshframework/fresh
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
freshframework/fresh#3920 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 82/100
freshframework/fresh#3833 ·
-
feat
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
freshframework/fresh#3803 · 1 comment ·
-
Difficulty 5/5 Over a week Newbie friendliness 10/100
freshframework/fresh#3918 · 1 comment · 4 reactions ·
-
Tab title doesn't update to <Head> <title> when navigating to a page with another <title>Possibly taken @1AbdullahButt claimed this 7 days ago. Open
Difficulty 3/5 1-2 days Newbie friendliness 57/100
freshframework/fresh#3916 ·
All issues in freshframework/fresh
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
tintinweb/pi-subagents#459 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
danielmiessler/LifeOS#2830 ·
Maintainers usually reply within 6 days
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
area: client enhancement priority: low
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
opensro-dev/opensro#3681 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Maintainers usually reply within 1 day