Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
2 tasks done
zt-9 opened this issue Oct 16, 2023 · 3 comments

Comments

@zt-9
Copy link

zt-9 commented Oct 16, 2023

Prerequisites

  • I have written a descriptive issue title
  • 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

@mcollina
Copy link
Member

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

@Neiz-Kap
Copy link

Neiz-Kap commented Aug 18, 2024

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
Copy link

fr1sk commented Sep 4, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants