solid-start: [Bug?]: The example in the docs doesn't seam to work.

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Current behavior 😯

The example in the documentation doesn’t seam to work.

declare module "@solidjs/start/server" {
  interface RequestEventLocals {
    myNumber: number;
    someString: string;
  }
}

locals property within the RequestEvent doesn’t have any types declared in global.d.ts. For example in one of the routes:

import { getRequestEvent } from 'solid-js/web'

const getMessage = cache(async () => {
  'use server'
  const event = getRequestEvent()
  // event?.locals is not typed
  return
}, 'message')

export const route = {
  load: () => getMessage(),
}

and my global.d.ts has

declare module '@solidjs/start/server' {
  interface RequestEventLocals {
    myNumber: number
    someString: string
  }
}

Expected behavior 🤔

No response

Steps to reproduce 🕹

Steps:

  1. Create an solid-start app using one of the templates
  2. Declare ‘@solidjs/start/server’ within the global.d.ts file
  3. Try accessing event.locals.<SOMETHING> within a route function

Context 🔦

No response

Your environment 🌎

No response

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 17 (9 by maintainers)

Most upvoted comments

It seems like the doc is not updated yet to use the new namespace App. I can write up a PR if the team wanted.

I also hope to add some additional comments on my usage of this feature:

  1. I believe the correct usage should be declare namespace App instead of declare module App (like in the current todosmvc example) -> LSP says A 'namespace' declaration should not be declared using the 'module' keyword. Please use the 'namespace' keyword instead when I use declare module App
  2. If one adds an import within global.d.ts file, the file turns into a module and its type become scoped/isolated. As a result, the App namespace loses its global-ness, and thus must be written inside a declare global. Technically this is a standard Typescript feature, but I do think it’s helpful to document this small “trick”

For example, here is my current global.d.ts

/// <reference types="@solidjs/start/env" />

import type { Request as CfRequest, D1Database, ExecutionContext } from '@cloudflare/workers-types'

declare global {
	namespace App {
		interface RequestEventLocals {
			db: D1Database
		}
	}
}

/**
 * Reference: https://developers.cloudflare.com/workers/runtime-apis/fetch-event/#parameters
 */
export interface CfPagesEnv {
	ASSETS: { fetch: (request: CfRequest) => Promise<Response> }
	CF_PAGES: '1'
	CF_PAGES_BRANCH: string
	CF_PAGES_COMMIT_SHA: string
	CF_PAGES_URL: string

	// Environment variables
	SECRET: string

	// Bindings
	DB: D1Database
}

declare module 'vinxi/http' {
	interface H3EventContext {
		cf: CfRequest['cf']
		cloudflare: {
			request: CfRequest
			env: CfPagesEnv
			context: ExecutionContext
		}
	}
}

The answer by @bhuynhdev is correct, the other fixes don’t work. The docs need an updated

@RonenEizen for me it started to work also in the global.d.ts when changing the /// <reference types="@solidjs/start/env" /> to import "@solidjs/start/env"; (make sure to restart ts server to see the effect)

I am not sure why this is the case though.