fix(appkit): allow undefined values in IGenieConfig.spaces#322
Open
jamesbroadhead wants to merge 1 commit intomainfrom
Open
fix(appkit): allow undefined values in IGenieConfig.spaces#322jamesbroadhead wants to merge 1 commit intomainfrom
jamesbroadhead wants to merge 1 commit intomainfrom
Conversation
The literal `genie({ spaces: { wanderbricks: process.env.X } })` does not
typecheck under tsc strict: `process.env.X` is `string | undefined`, but
`spaces` was typed `Record<string, string>`. Callers had to hoist into a
local with a runtime check, or use a non-null assertion, even though the
plugin's own resolveSpaceId already coalesces missing entries to null.
Loosen the type to `Record<string, string | undefined>` so env-driven
configs work without ceremony. Resolution behaviour is unchanged: aliases
mapped to undefined values fall through to "Unknown space alias" the same
way as missing keys, since `?? null` already handled both cases. New test
locks this in.
Signed-off-by: James Broadhead <jamesbroadhead@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
IGenieConfig.spacesis currently typed asRecord<string, string>, but the plugin's runtime contract is already permissive ofundefinedvalues.defaultSpaces()constructs the same map and freely returns{}when the env var is unset:When env is unset,
spaces.defaultisundefinedat runtime;?? nullcoalesces it; routes 404 with "Unknown space alias". That's the intended, working behaviour. The type just doesn't admit it.The mismatch surfaces the moment a caller wants more than one named alias and reaches for the env var directly:
The workarounds (
?? '',as string, hoist-and-narrow with a runtime throw) all paper over a type that's stricter than the code it describes.Change
spaces?: Record<string, string>→spaces?: Record<string, string | undefined>.defaultSpaces()return type updated to match.resolveSpaceIdis unchanged —?? nullalready handles undefined values identically to missing keys.Alternatives considered
as stringin the README. Type assertions silently mask config bugs at the callsite without changing runtime behaviour. Rejected — same hazard, less honest.spaceskeysstringand require non-empty values via Zod. Real validation, but a strictly bigger change and arguably the wrong layer — the plugin already returns 404 on miss, which is the validation step.Compatibility
Non-breaking. Function parameter types of
Record<K, T>are contravariant for callers: existing callers passingRecord<string, string>(a stricter map) still satisfyRecord<string, string | undefined>(a looser map).resolveSpaceId'sstring | nullreturn is unchanged.Test plan
pnpm -r typecheckpassesnpx vitest run packages/appkit/src/plugins/genie/tests/genie.test.ts— 30 passed, includes a new test asserting an explicitly-undefined alias 404s the same as a missing keynpx biome checkon touched files — cleanpnpm build— appkit + appkit-ui rebuild greenThis pull request and its description were written by Isaac.