fastify / fastify-env

Fastify plugin to check environment variables
MIT License
204 stars 26 forks source link

Property 'config' does not exist on type 'FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProviderDefault>'. #176

Open zt-9 opened 1 year ago

zt-9 commented 1 year ago

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.

// 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

mcollina commented 10 months ago

I think you need to add a TS reference block to load @fastify/env

Neiz-Kap commented 3 months ago

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>
fr1sk commented 2 months ago

I have the same issue, but would like to avoid those work arounds for a simple thing like getting env vars 🤷🏻‍♂️ any idea?