Replies: 2 comments 3 replies
-
|
You can achieve this with a global type declaration. Create a // solid.d.ts
import type * as SolidJS from "solid-js";
declare global {
namespace Solid {
export type Component<P = {}> = SolidJS.Component<P>;
export type ParentComponent<P = {}> = SolidJS.ParentComponent<P>;
export type ParentProps<P = {}> = SolidJS.ParentProps<P>;
export type FlowComponent<P = {}, C = SolidJS.JSX.Element> = SolidJS.FlowComponent<P, C>;
export type VoidComponent<P = {}> = SolidJS.VoidComponent<P>;
export type Accessor<T> = SolidJS.Accessor<T>;
export type Setter<T> = SolidJS.Setter<T>;
export type Signal<T> = SolidJS.Signal<T>;
export namespace JSX {
export type Element = SolidJS.JSX.Element;
export type IntrinsicElements = SolidJS.JSX.IntrinsicElements;
// Add others as needed
}
}
}
export {};Make sure the file is included in your {
"include": ["src", "solid.d.ts"]
}Now you can use const MyComponent: Solid.ParentComponent<{ title: string }> = (props) => {
return <div>{props.title}{props.children}</div>;
};The If you want to be exhaustive, you could also use mapped types to automatically re-export everything, but explicitly listing the types you use keeps things cleaner. |
Beta Was this translation helpful? Give feedback.
-
|
Ah, good point - you want to avoid manually listing them all. Unfortunately TypeScript doesn't support "re-exporting a namespace as a global" directly. The closest you can get to a fully automated solution: Option 1: Use a path alias (no global, but shorter import) // tsconfig.json
{
"compilerOptions": {
"paths": {
"Solid": ["node_modules/solid-js"]
}
}
}Then: Option 2: Editor snippet Create a snippet for Option 3: The nuclear option - patch the package types You could use Why there's no clean solution: TypeScript's // This doesn't exist
declare global {
export * as Solid from 'solid-js';
}This has been requested in TypeScript issues but hasn't been implemented. The manual re-export approach I showed earlier is genuinely the only way to get a true global If you only use a handful of types frequently, listing just those might be acceptable. But for the full namespace - yeah, you're stuck with the import unfortunately. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I like to use
Solid.Component/Solid.ParentComponent/Solid.ParentProps/Solid.JSX.Element, and having to doimport type * as Solid from 'solid-js'is a bit of a mouthful. I've tried a few things locally to make things easier, but short of repeating every type exported from solid-js I couldn't get anything working.Beta Was this translation helpful? Give feedback.
All reactions