mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-10-04 14:57:10 +00:00
Merge pull request #26479 from Hotell/mh/webpack-config-utils
[webpack-config-utils]: add type definitions
This commit is contained in:
+40
@@ -0,0 +1,40 @@
|
||||
// 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
|
||||
|
||||
export function getIfUtils<E extends EnvVars | string>(
|
||||
env: { [P in E]: boolean | string } | E,
|
||||
vars?: Array<EnvVars | string>
|
||||
): IfUtils;
|
||||
export function removeEmpty<T>(input: Array<T | undefined>): T[];
|
||||
export function removeEmpty<T>(input: { [P in keyof T]: T[P] }): NonEmptyObject<T>;
|
||||
export function propIf<E>(a: Falsy, value: any, alternate: E): E;
|
||||
export function propIf<I>(a: any, value: I, alternate: any): I;
|
||||
export function propIfNot<I>(a: Falsy, value: I, alternate: any): I;
|
||||
export function propIfNot<E>(a: any, value: any, alternate: E): E;
|
||||
|
||||
export type Falsy = false | '' | 'false' | undefined | null | 0;
|
||||
export type DefinedObjKeys<T> = ({ [P in keyof T]: T[P] extends undefined ? never : P })[keyof T];
|
||||
export type NonEmptyObject<T, P extends DefinedObjKeys<T> = DefinedObjKeys<T>> = { [PP in P]: T[PP] };
|
||||
|
||||
export type EnvVars = 'production' | 'prod' | 'test' | 'development' | 'dev';
|
||||
|
||||
export interface IfUtilsFn {
|
||||
<Y, N>(value: Y, alternate?: N): Y | N;
|
||||
(): boolean;
|
||||
}
|
||||
export 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;
|
||||
}
|
||||
@@ -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"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
@@ -0,0 +1,100 @@
|
||||
import webpackConfigUtils = require('webpack-config-utils');
|
||||
import { getIfUtils, removeEmpty, propIf, propIfNot } from 'webpack-config-utils';
|
||||
|
||||
{
|
||||
// propIf/propIfNot
|
||||
|
||||
// $ExpectType "value"
|
||||
propIf(true, 'value', 'alternate');
|
||||
|
||||
// $ExpectType "alternate"
|
||||
propIf(false, 'value', 'alternate');
|
||||
|
||||
// $ExpectType "alternate"
|
||||
propIf('false', 'value', 'alternate');
|
||||
|
||||
// $ExpectType "alternate"
|
||||
propIfNot(true, 'value', 'alternate');
|
||||
|
||||
// $ExpectType "value"
|
||||
propIfNot(false, 'value', 'alternate');
|
||||
|
||||
// $ExpectType "value"
|
||||
propIfNot('false', 'value', 'alternate');
|
||||
}
|
||||
{
|
||||
// getIfUtils
|
||||
{
|
||||
// $ExpectType IfUtils
|
||||
const utils = getIfUtils({});
|
||||
}
|
||||
{
|
||||
const utils = getIfUtils({}, ['foo', 'bar']); // 'ifFoo', 'ifNotFoo', 'ifBar', 'ifNotBar'
|
||||
const {
|
||||
ifFoo, // $ExpectType IfUtilsFn
|
||||
ifBar, // $ExpectType IfUtilsFn
|
||||
ifNotFoo, // $ExpectType IfUtilsFn
|
||||
ifNotBar // $ExpectType IfUtilsFn
|
||||
} = utils;
|
||||
}
|
||||
|
||||
{
|
||||
const {
|
||||
ifProduction // $ExpectType IfUtilsFn
|
||||
} = 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, // $ExpectType number
|
||||
b, // $ExpectType string
|
||||
d // $ExpectType null
|
||||
} = emptiedObject;
|
||||
|
||||
{
|
||||
// $ExpectError
|
||||
const {a, b, c, d} = emptiedObject;
|
||||
}
|
||||
|
||||
// $ExpectType number | null
|
||||
const firstItem = emptiedArray[0];
|
||||
}
|
||||
Reference in New Issue
Block a user