Fix type narrowing for nonEmptyArray (#185)

This commit is contained in:
Eugene 2023-07-14 23:24:56 +01:00 committed by GitHub
parent 9265e9072d
commit 3177d11801
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 12 deletions

View file

@ -1448,16 +1448,65 @@ test('is.nonEmptyArray', t => {
assert.nonEmptyArray(new Array()); // eslint-disable-line @typescript-eslint/no-array-constructor
});
// https://github.com/sindresorhus/is/issues/174
// {
// const strings = ['foo', 'bar']
// const function_ = (value: string) => value;
{
const strings = ['🦄', 'unicorn'];
const function_ = (value: string) => value;
// if (is.nonEmptyArray(strings)) {
// const value = strings[0]
// function_(value);
// }
// }
if (is.nonEmptyArray(strings)) {
const value = strings[0];
function_(value);
}
}
{
const mixed = ['🦄', 'unicorn', 1, 2];
const function_ = (value: string | number) => value;
if (is.nonEmptyArray(mixed)) {
const value = mixed[0];
function_(value);
}
}
{
const arrays = [['🦄'], ['unicorn']];
const function_ = (value: string[]) => value;
if (is.nonEmptyArray(arrays)) {
const value = arrays[0];
function_(value);
}
}
{
const strings = ['🦄', 'unicorn'];
const function_ = (value: string) => value;
assert.nonEmptyArray(strings);
const value = strings[0];
function_(value);
}
{
const mixed = ['🦄', 'unicorn', 1, 2];
const function_ = (value: string | number) => value;
assert.nonEmptyArray(mixed);
const value = mixed[0];
function_(value);
}
{
const arrays = [['🦄'], ['unicorn']];
const function_ = (value: string[]) => value;
assert.nonEmptyArray(arrays);
const value = arrays[0];
function_(value);
}
});
test('is.emptyString', t => {