My goal is to develop a library that automatically generates Typescript types for user code. I'd prefer to keep these type definitions in a separate folder for organizational purposes, while still allowing IDEs to recognize them.
While it's feasible with Typescript modules, I've been unable to figure out how to define types for an "ambient" module. Here's a simplified example of the desired file structure:
// src/pages/WelcomePage.ts
export const message = 'hi'
// src/index.ts
import { message } from './pages/WelcomePage'
console.log(message) // message should be of type "Message", not "string".
// types/pages/WelcomePage.d.ts
// This approach does not work.
// Is there a way to make it work?
declare module "pages/WelcomePage" {
type Message = string
export const message: Message
}
tldr: I want all my generated types to reside in types/...
and define types for src/...
.