Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.x] Utility types (#41246) #43108

Merged
merged 1 commit into from
Aug 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ module.exports = {
{
files: [
'.eslintrc.js',
'packages/kbn-utility-types/**/*',
'packages/kbn-eslint-plugin-eslint/**/*',
'packages/kbn-config-schema/**/*',
'packages/kbn-pm/**/*',
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@
"@kbn/expect": "1.0.0",
"@kbn/plugin-generator": "1.0.0",
"@kbn/test": "1.0.0",
"@kbn/utility-types": "1.0.0",
"@microsoft/api-documenter": "7.2.1",
"@microsoft/api-extractor": "7.1.8",
"@octokit/rest": "^15.10.0",
Expand Down
24 changes: 24 additions & 0 deletions packages/kbn-utility-types/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# `@kbn/utility-types`

TypeScript utility types for usage in Kibana.

- This package re-exports a subset of the items in [`utility-types`](https://github.com/piotrwitek/utility-types)
- You can also add more utility types here.


## Usage

```ts
import { UnwrapPromise } from '@kbn/utility-types';

type A = Promise<string>;
type B = UnwrapPromise<A>; // string
```


## Reference

- `UnwrapPromise<T>` &mdash; Returns wrapped type of a promise.
- `UnwrapObservable<T>` &mdash; Returns wrapped type of an observable.
- `ShallowPromise<T>` &mdash; Same as `Promise` type, but it flat maps the wrapped type.
- `ObservableLike<T>` &mdash; Minimal interface for an object resembling an `Observable`.
44 changes: 44 additions & 0 deletions packages/kbn-utility-types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you 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 { PromiseType } from 'utility-types';

/**
* Returns wrapped type of a promise.
*/
export type UnwrapPromise<T extends Promise<any>> = PromiseType<T>;

/**
* Minimal interface for an object resembling an `Observable`.
*/
export interface ObservableLike<T> {
subscribe(observer: (value: T) => void): void;
}

/**
* Returns wrapped type of an observable.
*/
export type UnwrapObservable<T extends ObservableLike<any>> = T extends ObservableLike<infer U>
? U
: never;

/**
* Converts a type to a `Promise`, unless it is already a `Promise`. Useful when proxying the return value of a possibly async function.
*/
export type ShallowPromise<T> = T extends Promise<infer U> ? Promise<U> : Promise<T>;
21 changes: 21 additions & 0 deletions packages/kbn-utility-types/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "@kbn/utility-types",
"version": "1.0.0",
"private": true,
"license": "Apache-2.0",
"main": "target",
"types": "target/index.d.ts",
"scripts": {
"build": "tsc",
"kbn:bootstrap": "tsc",
"kbn:watch": "tsc --watch",
"test": "tsd",
"clean": "rimraf target"
},
"dependencies": {
"utility-types": "^3.7.0"
},
"devDependencies": {
"tsd": "^0.7.4"
}
}
31 changes: 31 additions & 0 deletions packages/kbn-utility-types/test-d/shallow_promise.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you 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 { expectType } from 'tsd';
import { ShallowPromise } from '../index';

type P1 = ShallowPromise<string>;
type P2 = ShallowPromise<ShallowPromise<string>>;
type P3 = ShallowPromise<ShallowPromise<ShallowPromise<string>>>;
type P4 = ShallowPromise<ShallowPromise<ShallowPromise<number>>>;

expectType<P1>(Promise.resolve<string>('a'));
expectType<P2>(Promise.resolve<string>('a'));
expectType<P3>(Promise.resolve<string>('a'));
expectType<P4>(Promise.resolve<number>(123));
25 changes: 25 additions & 0 deletions packages/kbn-utility-types/test-d/unwrap_observable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you 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 { expectType } from 'tsd';
import { UnwrapObservable, ObservableLike } from '../index';

type STRING = UnwrapObservable<ObservableLike<string>>;

expectType<STRING>('adf');
27 changes: 27 additions & 0 deletions packages/kbn-utility-types/test-d/unwrap_promise.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you 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 { expectType } from 'tsd';
import { UnwrapPromise } from '../index';

type STRING = UnwrapPromise<Promise<string>>;
type TUPLE = UnwrapPromise<Promise<[number, number]>>;

expectType<STRING>('adf');
expectType<TUPLE>([1, 2]);
18 changes: 18 additions & 0 deletions packages/kbn-utility-types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"declaration": true,
"declarationDir": "./target",
"outDir": "./target",
"stripInternal": true,
"declarationMap": true,
"types": [
"jest",
"node"
]
},
"include": ["index.ts", "test-d/**/*"],
"exclude": [
"target"
]
}
1 change: 1 addition & 0 deletions src/dev/precommit_hook/casing_check_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export const IGNORE_FILE_GLOBS = [
'**/{webpackShims,__mocks__}/**/*',
'x-pack/docs/**/*',
'src/legacy/ui/public/assets/fonts/**/*',
'packages/kbn-utility-types/test-d/**/*',

// Files in this directory must match a pre-determined name in some cases.
'x-pack/legacy/plugins/canvas/.storybook/*',
Expand Down
5 changes: 3 additions & 2 deletions src/legacy/server/kbn_server.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

import { ResponseObject, Server } from 'hapi';
import { UnwrapPromise } from '@kbn/utility-types';

import { SavedObjectsClientProviderOptions } from 'src/core/server';
import {
Expand Down Expand Up @@ -87,7 +88,7 @@ declare module 'hapi' {
}

type KbnMixinFunc = (kbnServer: KbnServer, server: Server, config: any) => Promise<any> | void;
type Unpromise<T> = T extends Promise<infer U> ? U : T;

// eslint-disable-next-line import/no-default-export
export default class KbnServer {
public readonly newPlatform: {
Expand All @@ -104,7 +105,7 @@ export default class KbnServer {
};
stop: null;
params: {
handledConfigPaths: Unpromise<ReturnType<ConfigService['getUsedPaths']>>;
handledConfigPaths: UnwrapPromise<ReturnType<ConfigService['getUsedPaths']>>;
};
};
public server: Server;
Expand Down
1 change: 1 addition & 0 deletions x-pack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@kbn/plugin-helpers": "9.0.2",
"@kbn/pm": "1.0.0",
"@kbn/test": "1.0.0",
"@kbn/utility-types": "1.0.0",
"@mattapperson/slapshot": "1.2.3",
"@storybook/addon-actions": "^5.0.5",
"@storybook/addon-console": "^1.1.0",
Expand Down
Loading