DefinitelyTyped/types/webpack-config-utils/webpack-config-utils-tests.ts

101 lines
2.7 KiB
TypeScript

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];
}