Open zt-9 opened 1 year ago
I think you need to add a TS reference block to load @fastify/env
I can say, that fastify.getEnvs()
worked, but without specifying env variable like fastify.getEnvs().API_PORT
is marked as wrong for TypeScript
It helped me perfectly
declare module 'fastify' {
export interface FastifyInstance {
config: EnvSchema
}
}
But you need the type EnvSchema
, which one you may create manually or not to duplicate and generate by json-schema-to-ts
import { FromSchema, type JSONSchema } from 'json-schema-to-ts'
const schema = {
type: 'object',
required: ['DATABASE_URL', 'JWT_SECRET'],
properties: {
API_HOST: {
type: 'string',
default: '0.0.0.0',
},
API_PORT: {
type: 'number',
default: 3000,
},
DATABASE_URL: {
type: 'string',
},
LOG_LEVEL: {
type: 'string',
enum: ['fatal', 'error', 'warn', 'info', 'debug', 'trace', 'silent'], // "fatal" | "error" | "warn" | "info" | "debug" | "trace"
default: 'info',
},
NODE_ENV: {
type: 'string',
enum: ['development', 'production', 'test'],
default: 'production',
},
},
} as const satisfies JSONSchema
export type EnvSchema = FromSchema<typeof schema>
I have the same issue, but would like to avoid those work arounds for a simple thing like getting env vars 🤷🏻♂️ any idea?
Prerequisites
Fastify version
^4.24.2"
Plugin version
^4.2.0
Node.js version
v18.12.0
Operating system
macOS
Operating system version (i.e. 20.04, 11.3, 10)
14.0 (23A344)
Description
The code structure is created with fastify-cli using typescript template.
Steps to Reproduce
The code structure is created with fastify-cli using typescript template. maybe the error is cause by
@fastify/env
not extending the fastify instance object like thisMy code
I already registerred the "@fastify/env"
is it bec
Expected Behavior
No response