///
import {
DiContainer,
DiContainerForInjection,
Factory,
Injectable,
Injectable2,
InjectionToken,
InjectionToken2Base,
SpecificInjectionToken,
} from '@ogre-tools/injectable';
// A hook injects a single implementation, so a token it is given must be
// declared 'one'; the many-cardinalities go through `di.injectMany`. Pinned
// to `undefined` (not `any`) for the factory slot: a token with a factory is
// abstract, and a hook can't render an abstract token — see the comment on
// core's `InjectionToken2`.
export function useInject(
injectable: Injectable2 | (InjectionToken2Base & { readonly __abstract?: never }),
...params: Parameters
): Awaited>;
export function useInject(
injectable: Injectable | InjectionToken,
): Awaited;
export function useInject(
injectable:
| Injectable
| InjectionToken,
instantiationParameter: TInstantiationParameter,
): Awaited;
export function useInjectDeferred(
injectable: Injectable2 | (InjectionToken2Base & { readonly __abstract?: never }),
...params: Parameters
): Awaited>;
export function useInjectDeferred(
injectable: Injectable | InjectionToken,
): Awaited;
export function useInjectDeferred(
injectable:
| Injectable
| InjectionToken,
instantiationParameter: TInstantiationParameter,
): Awaited;
export function useInject2(alias: Injectable2): F;
export function useInject2(
alias: (InjectionToken2Base & { readonly __abstract?: never }),
): F;
export function useInject2(
alias: Injectable | InjectionToken,
): () => TReturnValue;
export function useInject2(
alias:
| Injectable
| InjectionToken,
): (
...params: TInstantiationParameter extends any[]
? TInstantiationParameter
: [TInstantiationParameter]
) => TReturnValue;
export type InjectableComponent> =
Component & Injectable;
type ExcludedKeys = 'instantiate' | 'lifecycle' | 'scope' | 'injectionToken' | "aliasType";
export declare function getInjectableComponent<
Component extends React.ComponentType
>(
injectable: Omit, ExcludedKeys> & {
id: string;
Component: Component;
PlaceholderComponent?: React.ComponentType>;
injectionToken?: InjectionToken | InjectionToken ? any : React.ComponentProps
>>;
},
): InjectableComponent;
export type InjectableComponent2> =
Component & Injectable2<() => Component>;
type ExcludedKeys2 = 'aliasType' | 'instantiate' | 'injectionToken';
export declare function getInjectableComponent2<
Component extends React.ComponentType
>(
injectable: Omit Component>, ExcludedKeys2> & {
id: string;
Component: Component;
PlaceholderComponent?: React.ComponentType>;
injectionToken?: InjectionToken2Base<() => Component, any> & {
readonly __abstract?: never;
};
},
): InjectableComponent2;
export type SpecificInjectionTokenComponent<
Component extends React.ComponentType,
> = Component & SpecificInjectionToken;
export type InjectionTokenComponent<
Component extends React.ComponentType,
SpecificFactory extends (
...args: any[]
) => SpecificInjectionTokenComponent = (
id: string,
) => SpecificInjectionTokenComponent,
> = Component & InjectionToken;
export declare function getInjectionTokenComponent<
Component extends React.ComponentType,
SpecificFactory extends (
...args: any[]
) => SpecificInjectionTokenComponent = (
id: string,
) => SpecificInjectionTokenComponent,
>(options: {
id: string;
PlaceholderComponent?: React.ComponentType>;
specificInjectionTokenFactory?: SpecificFactory;
tags?: string[];
}): InjectionTokenComponent;
// Defaults to `any`, not a concrete factory shape: a bare
// `InjectionTokenComponent2` means "a token component, abstract
// or not" — see the comment on core's `InjectionToken2`. The factory's
// return type is bounded by `SpecificInjectionTokenComponent2`
// (declared below): every `.for()` factory must return something with
// `speciality`, same as core's `InjectionToken2` — enforced here so
// `buildTokenComponent`'s memoization-by-speciality always has something to
// key on.
//
// The `Component &` intersection only applies when not abstract: the
// runtime object is technically still a callable function either way (the
// same `target: ComponentForReact` is assigned regardless of whether a
// factory was given — see `buildTokenComponent`), but an abstract token
// component must not type-check as `React.ComponentType` — rendering it
// throws at runtime (`useInject` rejects abstract tokens). This must stay a
// naked conditional for the same distribute-over-`any` reason documented on
// core's `InjectionToken2`.
export type InjectionTokenComponent2<
Component extends React.ComponentType,
SpecificFactory extends
| undefined
| ((...args: any[]) => SpecificInjectionTokenComponent2) = any,
> = SpecificFactory extends undefined
? Component &
InjectionToken2Base<() => Component, () => Component[], 'one'> & {
readonly __abstract?: never;
}
: InjectionToken2Base<() => Component, () => Component[], 'one'> & {
readonly __abstract: true;
for: SpecificFactory;
};
// Defaults to no factory: the speciality-carrying overload of
// `getInjectionTokenComponent2` below builds these as leaves, with no
// further `.for()` of their own, unless given one explicitly — mirroring
// core's `SpecificInjectionToken2`. Built from `InjectionTokenComponent2`
// rather than repeating its conditional: when `SpecificFactory` is
// `undefined` this is the directly-renderable concrete leaf, `Component &
// ...token... & {speciality}`; otherwise it's the abstract,
// not-a-`Component` branch plus `speciality` — a specific token component
// that is itself a family root for nested specificity.
export type SpecificInjectionTokenComponent2<
Component extends React.ComponentType,
SpecificFactory extends
| undefined
| ((...args: any[]) => SpecificInjectionTokenComponent2) = undefined,
> = InjectionTokenComponent2 & { speciality: any };
export interface InjectionTokenComponentOptionsWithoutFactory<
Component extends React.ComponentType,
> {
id: string;
PlaceholderComponent?: React.ComponentType>;
tags?: string[];
}
// Returned by `getInjectionTokenComponent2(options)` below.
// Calling it with no arguments gives a token with no `.for` at all — see the
// comment on core's `InjectionToken2`; calling it with a factory value keeps
// that factory's own generic signature intact in `SpecificFactory` — an
// *optional* slot (property or positional parameter) collapses a generic
// factory's signature instead, which is why this is two genuine overloads
// rather than one optional parameter, and `SpecificFactory` must not have a
// default either — in a multi-level `.for()` family a default erases a
// nested generic factory's type parameters to `any` (see the comment on
// core's `InjectionToken2FactoryCallFromSlots`).
export interface InjectionTokenComponent2FactoryCall<
Component extends React.ComponentType,
> {
(): InjectionTokenComponent2;
<
SpecificFactory extends (
...args: any[]
) => SpecificInjectionTokenComponent2,
>(
specificInjectionTokenFactory: SpecificFactory,
): InjectionTokenComponent2;
}
// A single, non-curried call — `Component` is the only type parameter here,
// so there's nothing for its explicit type argument to force a default on.
// `getInjectionTokenComponent2(options)()` uses the default
// `.for(id)` factory; `getInjectionTokenComponent2(options)(specificInjectionTokenFactory)`
// keeps a generic factory's own signature intact. Declared before the
// explicit-SF escape hatch below: TS overload resolution commits to the
// first overload whose generics resolve at all (falling back to constraints
// when nothing forces inference), so with the explicit-SF overload first, a
// call giving neither `` nor `` (e.g.
// `getInjectionTokenComponent2({...})()`) would wrongly resolve
// `SpecificFactory` to that overload's own constraint instead of `undefined`.
export declare function getInjectionTokenComponent2<
Component extends React.ComponentType,
>(
options: InjectionTokenComponentOptionsWithoutFactory,
): InjectionTokenComponent2FactoryCall;
// getInjectionTokenComponent2(options)(specificInjectionTokenFactory?):
// the explicit-SF escape hatch, flattened (options first, factory an
// optional trailing call) rather than curried: since `SpecificFactory` is
// already fixed explicitly, nothing on the options call needs inferring
// from it. Needed for a `.for()` factory whose return type
// narrows the general contract per specifier (e.g. via `TypedSpecifierType`)
// in a way inference from a real value could never reconstruct — spelling
// `SpecificFactory` out here lets that type be declared without needing to
// construct a matching runtime value.
export declare function getInjectionTokenComponent2<
Component extends React.ComponentType,
SpecificFactory extends (
...args: any[]
) => SpecificInjectionTokenComponent2,
>(
options: InjectionTokenComponentOptionsWithoutFactory,
): (
specificInjectionTokenFactory?: SpecificFactory,
) => InjectionTokenComponent2;
// Mirrors `InjectionTokenComponent2FactoryCall`, narrowed to
// `SpecificInjectionTokenComponent2`: returned by the `speciality`-carrying
// overload of `getInjectionTokenComponent2` below, which folds what used to
// be the separate `getSpecificInjectionTokenComponent2` creator into
// `getInjectionTokenComponent2` itself.
export interface SpecificInjectionTokenComponent2FactoryCall<
Component extends React.ComponentType,
> {
(): SpecificInjectionTokenComponent2;
<
SpecificFactory extends (
...args: any[]
) => SpecificInjectionTokenComponent2,
>(
specificInjectionTokenFactory: SpecificFactory,
): SpecificInjectionTokenComponent2;
}
// getInjectionTokenComponent2(options) where options carries
// `speciality`: builds a specific token component directly, folding in what
// used to be the separate getSpecificInjectionTokenComponent2 creator —
// buildTokenComponent already threads `speciality` through to core's
// getInjectionToken2 regardless of which overload is called, so this needed
// no runtime change, only a type declaration. Unlike the old
// getSpecificInjectionTokenComponent2, this also accepts a factory (curried,
// same as every other getInjectionTokenComponent2 call), so a specific token
// component can itself be a family root for nested specificity — which
// makes it abstract too, same as any other factory-bearing token component
// (see the comment on `InjectionTokenComponent2`). Unlike core's equivalent
// speciality overload, this needs no cardinality-literal split: a
// component's cardinality is hardcoded to `'one'`, so there's no generic
// cardinality inference here to collapse.
export declare function getInjectionTokenComponent2<
Component extends React.ComponentType,
>(
options: InjectionTokenComponentOptionsWithoutFactory & {
speciality: any;
},
): SpecificInjectionTokenComponent2FactoryCall;
export const DiContextProvider: React.Provider;
/** @deprecated Use injection hooks (`useSyncInject`, `useInjectDeferAwait`, `useInjectAsReactive`) or `getInjectableComponent` instead. */
export interface WithInjectablesSyncOptions<
Dependencies extends object,
Props extends object,
> {
getProps: (di: DiContainerForInjection, props: Props) => Props & Dependencies;
}
/** @deprecated Use injection hooks (`useSyncInject`, `useInjectDeferAwait`, `useInjectAsReactive`) or `getInjectableComponent` instead. */
export interface WithInjectablesAsyncOptions<
Dependencies extends object,
Props extends object,
> {
getProps: (
di: DiContainerForInjection,
props: Props,
) => Promise;
getPlaceholder: React.FunctionComponent;
}
/** @deprecated Use injection hooks (`useSyncInject`, `useInjectDeferAwait`, `useInjectAsReactive`) or `getInjectableComponent` instead. */
export interface WithInjectables {
(
Component: React.ElementType,
options: WithInjectablesSyncOptions,
): React.FunctionComponent;
(
Component: React.ForwardRefExoticComponent<
Dependencies & Props & React.RefAttributes[
>,
options: WithInjectablesSyncOptions,
): React.ForwardRefExoticComponent>;
(
Component: React.ElementType,
options: WithInjectablesAsyncOptions,
): React.FunctionComponent;
(
Component: React.ForwardRefExoticComponent<
Dependencies & Props & React.RefAttributes][
>,
options: WithInjectablesAsyncOptions,
): React.ForwardRefExoticComponent>;
}
/** @deprecated Use injection hooks (`useSyncInject`, `useInjectDeferAwait`, `useInjectAsReactive`) or `getInjectableComponent` instead. */
export const withInjectables: WithInjectables;
]