Effect CommunityEC
Effect Community8mo ago
1 reply
colorless

HttpApiSchema.param AnyString type-error

I'm attempting to create a param that only allows state abbreviations as valid params

export const STATES = [
  { min: 35000, max: 36999, code: "AL", long: "Alabama" },
  // all the states
  { min: 82000, max: 83199, code: "WY", long: "Wyoming" },
] as const;

export const StateCodesSchema = Schema.Literal(
  ...STATES.map(({ code }) => code),
);

export type StateCode = typeof StateCodesSchema.Type; // "AL" | ...all other states | "WY"


now when I try to use it via HttpApiSchema.param

const stateCodeParam = HttpApiSchema.param("stateCode", StateCodesSchema); // type-error

I would get a type error
Type 'string' is not assignable to type '"AL" | ... | "WY"'. tsserver (2345) [10, 57]


So how would I ensure the Schema validation while coercing it back to AnyString?

This is my current version:
effect: 3.15.4
@effect/platform: 0.82.7(effect@3.15.4)

Thanks!
Was this page helpful?