From a1831f8c53e3d64a661101b7365e83df1de18fa3 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Sun, 26 Apr 2020 13:16:50 +0200 Subject: [PATCH] packages/core: add helper for inferring ApiFactory types --- packages/core/src/api/apis/helpers.ts | 26 ++++++++++++++++++++++++++ packages/core/src/api/apis/index.ts | 1 + packages/core/src/api/apis/types.ts | 8 ++++---- 3 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 packages/core/src/api/apis/helpers.ts diff --git a/packages/core/src/api/apis/helpers.ts b/packages/core/src/api/apis/helpers.ts new file mode 100644 index 0000000000..1a46781c58 --- /dev/null +++ b/packages/core/src/api/apis/helpers.ts @@ -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( + factory: ApiFactory, +): ApiFactory { + return factory; +} diff --git a/packages/core/src/api/apis/index.ts b/packages/core/src/api/apis/index.ts index 8dd982264d..9ab9a25e50 100644 --- a/packages/core/src/api/apis/index.ts +++ b/packages/core/src/api/apis/index.ts @@ -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'; diff --git a/packages/core/src/api/apis/types.ts b/packages/core/src/api/apis/types.ts index 129afdaaa5..c44fd804a5 100644 --- a/packages/core/src/api/apis/types.ts +++ b/packages/core/src/api/apis/types.ts @@ -30,8 +30,8 @@ export type ApiHolder = { get(api: ApiRef): T | undefined; }; -export type ApiFactory = { - implements: ApiRef; - deps: TypesToApiRefs; - factory(deps: D): I extends A ? I : never; +export type ApiFactory = { + implements: ApiRef; + deps: TypesToApiRefs; + factory(deps: Deps): Impl extends Api ? Impl : never; };