Property 'config' does not exist on type 'FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProviderDefault>'.
Prerequisites
- [X] I have written a descriptive issue title
- [X] I have searched existing issues to ensure the bug has not already been reported
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.
// file: authorization.ts
import { FastifyInstance } from "fastify"
const authorization = async (fastify:FastifyInstance, opts) => {
const { httpErrors, config } = fastify // got error: Property 'config' does not exist on type 'FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProviderDefault>'.
}
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 this
declare module 'fastify' {
export interface FastifyInstance {
someSupport(): string;
}
}
My code
// file: authorization.ts
import { FastifyInstance } from "fastify"
const authorization = async (fastify:FastifyInstance, opts) => {
const { httpErrors, config } = fastify // got error: Property 'config' does not exist on type 'FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProviderDefault>'.
}
I already registerred the "@fastify/env"
// file: app.ts
import Env from "@fastify/env"
const app: FastifyPluginAsync<AppOptions> = async (
fastify,
opts
): Promise<void> => {
// Place here your custom code!
// env
void fastify.register(Env, {
schema: S.object()
.prop("NODE_ENV", S.string().default("development"))
.valueOf(),
})
is it bec
Expected Behavior
No response
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?