packages/core: add helper for inferring ApiFactory types

This commit is contained in:
Patrik Oldsberg
2020-04-26 13:16:50 +02:00
parent 2526eebdf1
commit a1831f8c53
3 changed files with 31 additions and 4 deletions
+26
View File
@@ -0,0 +1,26 @@
/*
* Copyright 2020 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { ApiFactory } from './types';
// Used to infer types for a standalone ApiFactory that isn't immediately passed
// to another function.
// This function doesn't actually do anything, it's only used to infer types.
export function createApiFactory<Api, Impl, Deps>(
factory: ApiFactory<Api, Impl, Deps>,
): ApiFactory<Api, Impl, Deps> {
return factory;
}
+1
View File
@@ -19,5 +19,6 @@ export { default as ApiRegistry } from './ApiRegistry';
export { default as ApiTestRegistry } from './ApiTestRegistry';
export { default as ApiRef } from './ApiRef';
export * from './types';
export * from './helpers';
export * from './definitions';
export * from './implementations';
+4 -4
View File
@@ -30,8 +30,8 @@ export type ApiHolder = {
get<T>(api: ApiRef<T>): T | undefined;
};
export type ApiFactory<A, I, D> = {
implements: ApiRef<A>;
deps: TypesToApiRefs<D>;
factory(deps: D): I extends A ? I : never;
export type ApiFactory<Api, Impl, Deps> = {
implements: ApiRef<Api>;
deps: TypesToApiRefs<Deps>;
factory(deps: Deps): Impl extends Api ? Impl : never;
};