[webpack-config-utils]: add type definitions

This commit is contained in:
Martin Hochel 2018-06-12 13:57:32 +02:00
parent 3b8b13474e
commit f02e9ae4dd
4 changed files with 189 additions and 0 deletions

64
types/webpack-config-utils/index.d.ts vendored Normal file
View File

@ -0,0 +1,64 @@
// Type definitions for webpack-config-utils 2.3
// Project: https://github.com/kentcdodds/webpack-config-utils#readme
// Definitions by: Martin Hochel <https://github.com/hotell>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.8
/*~ You can declare properties of the module using const, let, or var */
// declare const getIfUtils: WebpackConfigUtils.GetIfUtils;
// declare const removeEmpty: WebpackConfigUtils.RemoveEmpty;
// declare const propIf: WebpackConfigUtils.PropIf;
// declare const propIfNot: WebpackConfigUtils.PropIfNot;
declare const api: WebpackConfigUtils.API;
export = api;
declare namespace WebpackConfigUtils {
type Falsy = false | '' | undefined | null | 0;
type DefinedObjKeys<T> = ({ [P in keyof T]: T[P] extends undefined ? never : P })[keyof T];
type NonEmptyObject<T, P extends DefinedObjKeys<T> = DefinedObjKeys<T>> = { [PP in P]: T[PP] };
type EnvVars = 'production' | 'prod' | 'test' | 'development' | 'dev';
interface RemoveEmpty {
<T>(input: Array<T | undefined>): T[];
<T>(input: { [P in keyof T]: T[P] }): NonEmptyObject<T>;
}
type GetIfUtils = <E extends EnvVars | string>(
env: { [P in E]: boolean | string } | E,
vars?: Array<EnvVars | string>
) => IfUtils;
// @TODO
// with following defintion, generics will get flattened to base type -> string. Any ideas why or how to fix this?
// $ExpectType "value" | "alternate"
// propIf(true, 'value', 'alternate'); // 'value'
//
// type PropIf = <A, I, E>(add: A, value: I, alternate: E) => A extends Falsy ? E : I;
// type PropIfNot = <A, I, E>(add: A, value: I, alternate: E) => A extends Falsy ? I : E;
type PropIf = <I, E>(add: any, value: I, alternate: E) => I | E;
type PropIfNot = PropIf;
interface IfUtilsFn {
<Y, N>(value: Y, alternate?: N): Y | N;
(): boolean;
}
interface IfUtils {
ifDevelopment: IfUtilsFn;
ifNotDevelopment: IfUtilsFn;
ifDev: IfUtilsFn;
ifNotDev: IfUtilsFn;
ifProduction: IfUtilsFn;
ifNotProduction: IfUtilsFn;
ifProd: IfUtilsFn;
ifNotProd: IfUtilsFn;
ifTest: IfUtilsFn;
ifNotTest: IfUtilsFn;
[key: string]: IfUtilsFn;
}
interface API {
getIfUtils: GetIfUtils;
removeEmpty: RemoveEmpty;
propIf: PropIf;
propIfNot: PropIfNot;
}
}

View File

@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"webpack-config-utils-tests.ts"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }

View File

@ -0,0 +1,101 @@
import webpackConfigUtils = require('webpack-config-utils');
import { getIfUtils, removeEmpty, propIf, propIfNot } from 'webpack-config-utils';
{
// propIf/propIfNot
// $ExpectType "value" | "alternate"
propIf(true, 'value', 'alternate'); // 'value'
// $ExpectType "value" | "alternate"
propIf(false, 'value', 'alternate'); // 'alternate'
// $ExpectType "value" | "alternate"
propIf('false', 'value', 'alternate'); // 'alternate'
// $ExpectType "value" | "alternate"
propIfNot(true, 'value', 'alternate'); // 'alternate'
// $ExpectType "value" | "alternate"
propIfNot(false, 'value', 'alternate'); // 'value'
// $ExpectType "value" | "alternate"
propIfNot('false', 'value', 'alternate'); // 'value'
}
{
// getIfUtils
{
const expectedMethods = [
'ifProduction',
'ifNotProduction',
'ifProd',
'ifNotProd',
'ifTest',
'ifNotTest',
'ifDevelopment',
'ifNotDevelopment',
'ifDev',
'ifNotDev'
];
const utils = getIfUtils({});
}
{
const expectedMethods = ['ifFoo', 'ifNotFoo', 'ifBar', 'ifNotBar'];
const utils = getIfUtils({}, ['foo', 'bar']);
const { ifFoo, ifBar, ifNotFoo, ifNotBar } = utils;
}
{
const { ifProduction } = getIfUtils('production');
// $ExpectType "value" | "alternate"
ifProduction('value', 'alternate'); // 'value'
}
{
const { ifNotDev } = getIfUtils({ dev: false });
// $ExpectType "value" | "alternate"
ifNotDev('value', 'alternate'); // 'value'
}
{
// $ExpectError
getIfUtils(false); // webpack-config-utils:getIfUtils.*?string\/Object/);
}
{
const { ifTest, ifProd, ifNotDev } = getIfUtils('test');
// $ExpectType boolean
ifTest(); // true;
// $ExpectType boolean
ifProd(); // false;
// $ExpectType boolean
ifNotDev(); // true;
}
{
const { ifWatch, ifProd, ifNotDev, ifTest } = getIfUtils('watch', ['prod', 'dev', 'watch']);
// $ExpectType boolean
ifWatch(); // true
// $ExpectType boolean
ifProd(); // false
// $ExpectType boolean
ifNotDev(); // true
// $ExpectType IfUtilsFn
ifTest; // function
}
}
{
// removeEmpty
// $ExpectType (number | null)[]
const emptiedArray = removeEmpty([undefined, 0, 1, 2, undefined, 3, undefined, null]); // [0, 1, 2, 3, null]
// $ExpectType NonEmptyObject<{ a: number; b: string; c: undefined; d: null; }, "b" | "a" | "d">
const emptiedObject = removeEmpty({ a: 1, b: 'b', c: undefined, d: null }); // {a: 1, b: 'b', d: null}
const {a, b, d} = emptiedObject;
{
// $ExpectError
const {a, b, c, d} = emptiedObject; // Error
}
// $ExpectType number | null
const firstItem = emptiedArray[0];
}