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

* feat: 🎸 add @kbn/utility-types

* feat: 🎸 improve @kbn/utility-types

* chore: 🤖 move @kbn/utility-types to dev deps

* chore: 🤖 change @kbn/utility-types build setup

* fix: 🐛 implement review suggestions

* feat: 🎸 add ShallowPromise type

* Update packages/kbn-utility-types/README.md

Co-Authored-By: Luke Elmers <lukeelmers@gmail.com>

* test: 💍 add tests for utility-types

* chore: 🤖 add utility-types tests to TypeScript config

* test: 💍 remove negative tests to not cause TypeScript fail

* chore: 🤖 remove ref to type defs to try fix CI tests

* Update packages/kbn-utility-types/index.ts

Co-Authored-By: Spencer <email@spalger.com>

* chore: 🤖 add TS types index to fix `grunt run:test_projects`

* chore: 🤖 use similar tsconfig.json as in other packages

* chore: 🤖 add "clean" script

* chore: 🤖 add kbn:bootstrap script
This commit is contained in:
Vadim Dalecky 2019-08-12 16:57:17 +02:00 committed by GitHub
parent 690a1306bb
commit c868741af1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 281 additions and 8 deletions

View file

@ -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/**/*',

View file

@ -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",

View file

@ -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`.

View file

@ -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>;

View file

@ -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"
}
}

View file

@ -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));

View file

@ -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');

View file

@ -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]);

View file

@ -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"
]
}

View file

@ -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/*',

View file

@ -18,6 +18,7 @@
*/
import { ResponseObject, Server } from 'hapi';
import { UnwrapPromise } from '@kbn/utility-types';
import { SavedObjectsClientProviderOptions } from 'src/core/server';
import {
@ -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: {
@ -104,7 +105,7 @@ export default class KbnServer {
};
stop: null;
params: {
handledConfigPaths: Unpromise<ReturnType<ConfigService['getUsedPaths']>>;
handledConfigPaths: UnwrapPromise<ReturnType<ConfigService['getUsedPaths']>>;
};
};
public server: Server;

View file

@ -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",

View file

@ -5260,6 +5260,11 @@ ansi-escapes@^1.0.0, ansi-escapes@^1.1.0:
resolved "https://registry.yarnpkg.com/ansi-escapes/-/ansi-escapes-1.4.0.tgz#d3a8a83b319aa67793662b13e761c7911422306e"
integrity sha1-06ioOzGapneTZisT52HHkRQiMG4=
ansi-escapes@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ansi-escapes/-/ansi-escapes-2.0.0.tgz#5bae52be424878dd9783e8910e3fc2922e83c81b"
integrity sha1-W65SvkJIeN2Xg+iRDj/Cki6DyBs=
ansi-escapes@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/ansi-escapes/-/ansi-escapes-3.0.0.tgz#ec3e8b4e9f8064fc02c3ac9b65f1c275bda8ef92"
@ -9937,7 +9942,15 @@ debuglog@^1.0.1:
resolved "https://registry.yarnpkg.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492"
integrity sha1-qiT/uaw9+aI1GDfPstJ5NgzXhJI=
decamelize@^1.0.0, decamelize@^1.1.1, decamelize@^1.1.2, decamelize@^1.2.0:
decamelize-keys@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/decamelize-keys/-/decamelize-keys-1.1.0.tgz#d171a87933252807eb3cb61dc1c1445d078df2d9"
integrity sha1-0XGoeTMlKAfrPLYdwcFEXQeN8tk=
dependencies:
decamelize "^1.1.0"
map-obj "^1.0.0"
decamelize@^1.0.0, decamelize@^1.1.0, decamelize@^1.1.1, decamelize@^1.1.2, decamelize@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/decamelize/-/decamelize-1.2.0.tgz#f6534d15148269b20352e7bee26f501f9a191290"
integrity sha1-9lNNFRSCabIDUue+4m9QH5oZEpA=
@ -11379,6 +11392,17 @@ eslint-config-prettier@6.0.0:
dependencies:
get-stdin "^6.0.0"
eslint-formatter-pretty@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/eslint-formatter-pretty/-/eslint-formatter-pretty-1.3.0.tgz#985d9e41c1f8475f4a090c5dbd2dfcf2821d607e"
integrity sha512-5DY64Y1rYCm7cfFDHEGUn54bvCnK+wSUVF07N8oXeqUJFSd+gnYOTXbzelQ1HurESluY6gnEQPmXOIkB4Wa+gA==
dependencies:
ansi-escapes "^2.0.0"
chalk "^2.1.0"
log-symbols "^2.0.0"
plur "^2.1.2"
string-width "^2.0.0"
eslint-import-resolver-node@0.3.2, eslint-import-resolver-node@^0.3.2:
version "0.3.2"
resolved "https://registry.yarnpkg.com/eslint-import-resolver-node/-/eslint-import-resolver-node-0.3.2.tgz#58f15fb839b8d0576ca980413476aab2472db66a"
@ -13808,7 +13832,7 @@ globby@^8.0.1:
pify "^3.0.0"
slash "^1.0.0"
globby@^9.2.0:
globby@^9.1.0, globby@^9.2.0:
version "9.2.0"
resolved "https://registry.yarnpkg.com/globby/-/globby-9.2.0.tgz#fd029a706c703d29bdd170f4b6db3a3f7a7cb63d"
integrity sha512-ollPHROa5mcxDEkwg6bPt3QbEf4pDQSNtd6JPL1YvOvAo/7/0VAm9TccUeoTmarjPw4pfUthSCqcyfNB1I3ZSg==
@ -18630,7 +18654,7 @@ log-ok@^0.1.1:
ansi-green "^0.1.1"
success-symbol "^0.1.0"
log-symbols@2.2.0, log-symbols@^2.1.0, log-symbols@^2.2.0:
log-symbols@2.2.0, log-symbols@^2.0.0, log-symbols@^2.1.0, log-symbols@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/log-symbols/-/log-symbols-2.2.0.tgz#5740e1c5d6f0dfda4ad9323b5332107ef6b4c40a"
integrity sha512-VeIAFslyIerEJLXHziedo2basKbMKtTw3vfn5IzG0XTjhAVEJyNHnL2p7vc+wBDSdQuUpNw3M2u6xb9QsAY5Eg==
@ -19157,6 +19181,21 @@ meow@^3.0.0, meow@^3.3.0, meow@^3.7.0:
redent "^1.0.0"
trim-newlines "^1.0.0"
meow@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/meow/-/meow-5.0.0.tgz#dfc73d63a9afc714a5e371760eb5c88b91078aa4"
integrity sha512-CbTqYU17ABaLefO8vCU153ZZlprKYWDljcndKKDCFcYQITzWCXZAVk4QMFZPgvzrnUQ3uItnIE/LoUOwrT15Ig==
dependencies:
camelcase-keys "^4.0.0"
decamelize-keys "^1.0.0"
loud-rejection "^1.0.0"
minimist-options "^3.0.1"
normalize-package-data "^2.3.4"
read-pkg-up "^3.0.0"
redent "^2.0.0"
trim-newlines "^2.0.0"
yargs-parser "^10.0.0"
merge-deep@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/merge-deep/-/merge-deep-3.0.2.tgz#f39fa100a4f1bd34ff29f7d2bf4508fbb8d83ad2"
@ -19416,6 +19455,14 @@ minimatch@~0.2.11:
lru-cache "2"
sigmund "~1.0.0"
minimist-options@^3.0.1:
version "3.0.2"
resolved "https://registry.yarnpkg.com/minimist-options/-/minimist-options-3.0.2.tgz#fba4c8191339e13ecf4d61beb03f070103f3d954"
integrity sha512-FyBrT/d0d4+uiZRbqznPXqw3IpZZG3gl3wKWiX784FycUKVwBt0uLBFkQrtE4tZOrgo78nZp2jnKz3L65T5LdQ==
dependencies:
arrify "^1.0.1"
is-plain-obj "^1.1.0"
minimist@0.0.5:
version "0.0.5"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.5.tgz#d7aa327bcecf518f9106ac6b8f003fa3bcea8566"
@ -21663,7 +21710,7 @@ plugin-error@^1.0.1:
arr-union "^3.1.0"
extend-shallow "^3.0.2"
plur@^2.1.0:
plur@^2.1.0, plur@^2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/plur/-/plur-2.1.2.tgz#7482452c1a0f508e3e344eaec312c91c29dc655a"
integrity sha1-dIJFLBoPUI4+NE6uwxLJHCncZVo=
@ -23540,6 +23587,14 @@ read-pkg-up@^2.0.0:
find-up "^2.0.0"
read-pkg "^2.0.0"
read-pkg-up@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/read-pkg-up/-/read-pkg-up-3.0.0.tgz#3ed496685dba0f8fe118d0691dc51f4a1ff96f07"
integrity sha1-PtSWaF26D4/hGNBpHcUfSh/5bwc=
dependencies:
find-up "^2.0.0"
read-pkg "^3.0.0"
read-pkg-up@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/read-pkg-up/-/read-pkg-up-4.0.0.tgz#1b221c6088ba7799601c808f91161c66e58f8978"
@ -27249,6 +27304,11 @@ trim-newlines@^1.0.0:
resolved "https://registry.yarnpkg.com/trim-newlines/-/trim-newlines-1.0.0.tgz#5887966bb582a4503a41eb524f7d35011815a613"
integrity sha1-WIeWa7WCpFA6QetST301ARgVphM=
trim-newlines@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/trim-newlines/-/trim-newlines-2.0.0.tgz#b403d0b91be50c331dfc4b82eeceb22c3de16d20"
integrity sha1-tAPQuRvlDDMd/EuC7s6yLD3hbSA=
trim-repeated@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/trim-repeated/-/trim-repeated-1.0.0.tgz#e3646a2ea4e891312bf7eace6cfb05380bc01c21"
@ -27349,6 +27409,19 @@ ts-log@2.1.3:
resolved "https://registry.yarnpkg.com/ts-log/-/ts-log-2.1.3.tgz#9e30aca1baffe7693a2e4142b8f07ecb01cb8340"
integrity sha512-VIk9+hzE80UjhJcSANst8LGRBpfNh32y9d3LVDMtEqcEb1x0hB71IO0aObNcLJ5VpK5tKeF9uI4pwEco03SkwA==
tsd@^0.7.4:
version "0.7.4"
resolved "https://registry.yarnpkg.com/tsd/-/tsd-0.7.4.tgz#d9aba567f1394641821a6800dcee60746c87bd03"
integrity sha512-cqr1s2GHtVkU3L/4BXDaeJOjFEuZ7iOVC+hwmyx4G7Eo26mSXCFNnwFm4EasK/MW2HdY3AQWux+AjYzDYLzZow==
dependencies:
eslint-formatter-pretty "^1.3.0"
globby "^9.1.0"
meow "^5.0.0"
path-exists "^3.0.0"
read-pkg-up "^4.0.0"
typescript "^3.0.1"
update-notifier "^2.5.0"
tslib@1.9.3, tslib@^1.8.1, tslib@^1.9.0, tslib@^1.9.2, tslib@^1.9.3:
version "1.9.3"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.9.3.tgz#d7e4dd79245d85428c4d7e4822a79917954ca286"
@ -27913,7 +27986,7 @@ typescript-fsa@^2.0.0, typescript-fsa@^2.5.0:
resolved "https://registry.yarnpkg.com/typescript-fsa/-/typescript-fsa-2.5.0.tgz#1baec01b5e8f5f34c322679d1327016e9e294faf"
integrity sha1-G67AG16PXzTDImedEycBbp4pT68=
typescript@3.5.1, typescript@3.5.3, typescript@^3.0.3, typescript@^3.3.3333, typescript@^3.4.5, typescript@~3.0.3, typescript@~3.3.3333, typescript@~3.4.3:
typescript@3.5.1, typescript@3.5.3, typescript@^3.0.1, typescript@^3.0.3, typescript@^3.3.3333, typescript@^3.4.5, typescript@~3.0.3, typescript@~3.3.3333, typescript@~3.4.3:
version "3.5.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.5.3.tgz#c830f657f93f1ea846819e929092f5fe5983e977"
integrity sha512-ACzBtm/PhXBDId6a6sDJfroT2pOWt/oOnk4/dElG5G33ZL776N3Y6/6bKZJBFpd+b05F3Ct9qDjMeJmRWtE2/g==
@ -28453,6 +28526,11 @@ utila@^0.4.0, utila@~0.4:
resolved "https://registry.yarnpkg.com/utila/-/utila-0.4.0.tgz#8a16a05d445657a3aea5eecc5b12a4fa5379772c"
integrity sha1-ihagXURWV6Oupe7MWxKk+lN5dyw=
utility-types@^3.7.0:
version "3.7.0"
resolved "https://registry.yarnpkg.com/utility-types/-/utility-types-3.7.0.tgz#51f1c29fa35d4267488345706efcf3f68f2b1933"
integrity sha512-mqRJXN7dEArK/NZNJUubjr9kbFFVZcmF/JHDc9jt5O/aYXUVmopHYujDMhLmLil1Bxo2+khe6KAIVvDH9Yc4VA==
utils-merge@1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/utils-merge/-/utils-merge-1.0.1.tgz#9f95710f50a267947b2ccc124741c1028427e713"
@ -29840,7 +29918,7 @@ yallist@^3.0.3:
resolved "https://registry.yarnpkg.com/yallist/-/yallist-3.0.3.tgz#b4b049e314be545e3ce802236d6cd22cd91c3de9"
integrity sha512-S+Zk8DEWE6oKpV+vI3qWkaK+jSbIK86pCwe2IF/xwIpQ8jEuxpw9NyaGjmp9+BoJv5FV2piqCDcoCtStppiq2A==
yargs-parser@^10.1.0:
yargs-parser@^10.0.0, yargs-parser@^10.1.0:
version "10.1.0"
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-10.1.0.tgz#7202265b89f7e9e9f2e5765e0fe735a905edbaa8"
integrity sha512-VCIyR1wJoEBZUqk5PA+oOBF6ypbwh5aNB3I50guxAL/quggdfs4TtNHQrSazFA3fYZ+tEqfs0zIGlv0c/rgjbQ==