Skip to content

Commit

Permalink
feat: Expose typing for instantiation parameter of computedInjectMany
Browse files Browse the repository at this point in the history
  • Loading branch information
Iku-turso committed Nov 27, 2023
1 parent 19312b7 commit e87d092
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
9 changes: 7 additions & 2 deletions packages/injectable/extension-for-mobx/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,28 @@ export function registerMobX(di: DiContainer): void;
export const computedInjectManyInjectable: Injectable<
<
TInjectionToken extends InjectionToken<any, any>,
TInstance = TInjectionToken extends InjectionToken<infer T, any>
TInstance extends TInjectionToken extends InjectionToken<infer T, any>
? T
: never,
>(
injectionToken: TInjectionToken,
...param: TInjectionToken extends InjectionToken<any, infer T> ? [T] : []
) => IComputedValue<TInstance[]>,
unknown
>;

export const computedInjectManyWithMetaInjectable: Injectable<
<
TInjectionToken extends InjectionToken<any, any>,
TInstanceWithMeta = TInjectionToken extends InjectionToken<infer T, any>
TInstanceWithMeta extends TInjectionToken extends InjectionToken<
infer T,
any
>
? InjectionInstanceWithMeta<T>
: never,
>(
injectionToken: TInjectionToken,
...param: TInjectionToken extends InjectionToken<any, infer T> ? [T] : []
) => IComputedValue<TInstanceWithMeta[]>,
unknown
>;
54 changes: 53 additions & 1 deletion packages/injectable/extension-for-mobx/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expectType } from 'tsd';
import { expectError, expectType } from 'tsd';

import {
createContainer,
Expand Down Expand Up @@ -32,3 +32,55 @@ const computedInjectManyWithMeta = di.inject(
expectType<IComputedValue<InjectionInstanceWithMeta<string>[]>>(
computedInjectManyWithMeta(someInjectionToken),
);

// Given token with an instantiation parameter...
export const someInjectionTokenWithParameter = getInjectionToken<
string,
number
>({
id: 'irrelevant',
});

// ...when using computedInjectMany, typing is ok.
expectType<IComputedValue<string[]>>(
computedInjectMany(someInjectionTokenWithParameter, 42),
);

// ...when using computedInjectManyWithMeta, typing is ok.
expectType<IComputedValue<InjectionInstanceWithMeta<string>[]>>(
computedInjectManyWithMeta(someInjectionTokenWithParameter, 42),
);

// ...given instantiation parameter of wrong type, when using computedInjectMany, typing is not ok.
expectError(
computedInjectMany(someInjectionTokenWithParameter),
'some-string-instead-of-number',
);

// ...given instantiation parameter of wrong type, when using computedInjectManyWithMeta, typing is not ok.
expectError(
computedInjectManyWithMeta(
someInjectionTokenWithParameter,
'some-string-instead-of-number',
),
);

// ...given instantiation parameter of wrong type, when using computedInjectMany, typing is not ok.
expectError(
computedInjectMany(someInjectionTokenWithParameter),
'some-string-instead-of-number',
);

// ...given instantiation parameter of wrong type, when using computedInjectManyWithMeta, typing is not ok.
expectError(
computedInjectManyWithMeta(
someInjectionTokenWithParameter,
'some-string-instead-of-number',
),
);

// ...given no instantiation parameter, when using computedInjectMany, typing is not ok.
expectError(computedInjectMany(someInjectionTokenWithParameter));

// ...given no instantiation parameter, when using computedInjectManyWithMeta, typing is not ok.
expectError(computedInjectManyWithMeta(someInjectionTokenWithParameter));

0 comments on commit e87d092

Please sign in to comment.