You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
it's impossible as-is to make a helper function that requires only one variable in the Env, and then pass to it one with several (i.e. because the route using the function has additional middleware).
import { Context } from 'hono';
type FooEnv = {
Variables: {
foo: string;
}
};
type BarEnv = {
Variables: {
boo: string;
}
};
async function util(c: Context<FooEnv>) {
return
}
var c: Context<FooEnv & BarEnv>;
util(c);
test.ts:21:6 - error TS2345: Argument of type 'Context<FooEnv & BarEnv, any, {}>' is not assignable to parameter of type 'Context<FooEnv, any, {}>'.
Type 'FooEnv' is not assignable to type 'FooEnv & BarEnv'.
Type 'FooEnv' is not assignable to type 'BarEnv'.
Types of property 'Variables' are incompatible.
Property 'boo' is missing in type '{ foo: string; }' but required in type '{ boo: string; }'.
I believe the problem is the way set's arguments are contravariant; it is a little more apparent when you imagine two environments which provide the same variable like type FooVars = { foo: A | B }; type BarVars = { foo: B | C }. (I'm not sure when this case would happen, however.)
Is there a recommended way to provide helper functions that are capable of receiving subtyped Contexts?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
it's impossible as-is to make a helper function that requires only one variable in the Env, and then pass to it one with several (i.e. because the route using the function has additional middleware).
I believe the problem is the way
set's arguments are contravariant; it is a little more apparent when you imagine two environments which provide the same variable liketype FooVars = { foo: A | B }; type BarVars = { foo: B | C }. (I'm not sure when this case would happen, however.)Is there a recommended way to provide helper functions that are capable of receiving subtyped Contexts?
Beta Was this translation helpful? Give feedback.
All reactions