False recursive reference

Anyone know how I can get past this false positive claiming I'm recursively referencing process.env in its base type? Since the value is just being used to compute the base type, not in the actual base type, this should be okay.
import { type } from "arktype";

const env = type({
...(process.env.NODE_ENV === "production" && {
DATABASE_URL: 'string'
}),
});

export const assertEnv = () => env.assert(process.env);

type Env = typeof env.infer;
declare global {
namespace NodeJS {
interface ProcessEnv extends Env {}
}
}
import { type } from "arktype";

const env = type({
...(process.env.NODE_ENV === "production" && {
DATABASE_URL: 'string'
}),
});

export const assertEnv = () => env.assert(process.env);

type Env = typeof env.infer;
declare global {
namespace NodeJS {
interface ProcessEnv extends Env {}
}
}
1 Reply
ssalbdivad
ssalbdivad12mo ago
TS struggles with this a bit since it's trying to use the inferred type for the process.env.NODE_ENV check. You can work around this by casting process.env on that line:
const env = type({
...((process.env as any).NODE_ENV === "production" && {
DATABASE_URL: "string"
})
})
const env = type({
...((process.env as any).NODE_ENV === "production" && {
DATABASE_URL: "string"
})
})
If you don't want to use any you can use Record<string, string | undefined> or create a RawProcessEnv type, but it's pretty harmless in a case like this where it's can't propagate outside that single statement.

Did you find this page helpful?