|
| 1 | +// The shapes a component is DECLARED in, each rendered from app.tsx as a JSX tag. |
| 2 | +// `<X/>` is an invocation of X whatever X is bound to: a function, an arrow held by |
| 3 | +// a const, a default export, a class, or a wrapper's result around one of those. |
| 4 | +// |
| 5 | +// No React dependency: the wrappers are declared here with the signatures that matter. |
| 6 | +declare global { |
| 7 | + namespace JSX { |
| 8 | + type Element = string; |
| 9 | + interface ElementAttributesProperty { |
| 10 | + props: unknown; |
| 11 | + } |
| 12 | + interface IntrinsicElements { |
| 13 | + [name: string]: unknown; |
| 14 | + } |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +export type Fc<P> = (props: P) => string; |
| 19 | +export declare function memo<P>(component: Fc<P>): Fc<P>; |
| 20 | +export declare function forwardRef<P>(render: (props: P, ref: unknown) => string): Fc<P>; |
| 21 | +export declare function lazy<P>(load: () => Promise<{ default: Fc<P> }>): Fc<P>; |
| 22 | +export declare function makeFactory<T>(build: () => T): () => T; |
| 23 | + |
| 24 | +export function formatName(n: string): string { |
| 25 | + return n.toUpperCase(); |
| 26 | +} |
| 27 | + |
| 28 | +export function trackClick(id: string): string { |
| 29 | + return id; |
| 30 | +} |
| 31 | + |
| 32 | +// memo() around a NAMED function expression. |
| 33 | +export const UserCard = memo(function UserCard(p: { id: string }): string { |
| 34 | + return formatName(p.id); |
| 35 | +}); |
| 36 | + |
| 37 | +// forwardRef() around a function declared elsewhere, passed by name. |
| 38 | +function FieldImpl(p: { label: string }, ref: unknown): string { |
| 39 | + return trackClick(p.label) + String(ref); |
| 40 | +} |
| 41 | +export const Field = forwardRef(FieldImpl); |
| 42 | + |
| 43 | +// An arrow held by a typed const. |
| 44 | +export const Badge: Fc<{ n: number }> = (p) => String(p.n); |
| 45 | + |
| 46 | +// A default export. |
| 47 | +export default function Avatar(p: { src: string }): string { |
| 48 | + return p.src; |
| 49 | +} |
| 50 | + |
| 51 | +// A class component: the constructor is the nameable target. |
| 52 | +export class Panel { |
| 53 | + props: { title: string }; |
| 54 | + constructor(props: { title: string }) { |
| 55 | + this.props = props; |
| 56 | + } |
| 57 | + render(): string { |
| 58 | + return this.props.title; |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +export const ui = { |
| 63 | + Chip(p: { text: string }): string { |
| 64 | + return p.text; |
| 65 | + }, |
| 66 | +}; |
| 67 | + |
| 68 | +// CONTROL: a non-JSX wrapper. A factory's argument 0 is not what its product runs |
| 69 | +// when called, so `factory()` must not become a call to buildDefault. |
| 70 | +export function buildDefault(): number { |
| 71 | + return 1; |
| 72 | +} |
| 73 | +export const factory = makeFactory(buildDefault); |
0 commit comments