Skip to content

Commit

Permalink
fix: correct various type errors
Browse files Browse the repository at this point in the history
Correctly handle `null` arguments.
  • Loading branch information
nikku committed Jan 17, 2024
1 parent 667a82b commit e13dc95
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/array.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
*
* @return
*/
export function flatten<T extends any[]>(arr: T[]): T;
export function flatten<T extends any>(arr: T[] | null): T[];
2 changes: 1 addition & 1 deletion lib/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
* @template T
*
* @param {T[][]} arr
* @param {T[][] | T[] | null} [arr]
*
* @return {T[]}
*/
Expand Down
2 changes: 1 addition & 1 deletion lib/collection.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type ArrayCollection<T> = Array<T>;
export type StringKeyValueCollection<T> = { [key: string]: T };
export type NumberKeyValueCollection<T> = { [key: number]: T };
export type KeyValueCollection<T> = StringKeyValueCollection<T> | NumberKeyValueCollection<T>;
export type Collection<T> = KeyValueCollection<T> | ArrayCollection<T>;
export type Collection<T> = KeyValueCollection<T> | ArrayCollection<T> | null | undefined;

/**
* Find element in collection.
Expand Down
4 changes: 2 additions & 2 deletions lib/lang.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {

export function isUndefined(obj: any): obj is null | undefined;
export function isDefined(obj: any): obj is Exclude<any, null | undefined>;
export function isNil(obj: any): obj is object;
export function isArray(obj: any): obj is Array<any>;
export function isNil(obj: any): value is null | undefined;
export function isArray(obj: any): obj is Array;
export function isObject(obj: any): obj is object;
export function isNumber(obj: any): obj is number;
export function isFunction(obj: any): obj is Function;
Expand Down
10 changes: 10 additions & 0 deletions test/array.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ describe('array', function() {

describe('flatten', function() {

it('should handle null values', function() {

// then
expect(flatten(null)).to.eql([]);

// @ts-ignore-error "missing arg"
expect(flatten()).to.eql([]);
});


it('should flatten, one level deep', function() {

// given
Expand Down
4 changes: 4 additions & 0 deletions test/fn.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ describe('fn', function() {

// given
let fn = function() {

// @ts-ignore-error "this"
return this.foo;
};

Expand Down Expand Up @@ -110,6 +112,8 @@ describe('fn', function() {
let self = {};

let callback = sinon.spy(function() {

// @ts-ignore-error "this"
expect(this).to.equal(self);
});

Expand Down
4 changes: 4 additions & 0 deletions test/lang.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ describe('lang', function() {

expect(isDefined(null)).to.be.true;

// @ts-ignore-error "missing arg"
expect(isDefined()).to.be.false;
expect(isDefined(undefined)).to.be.false;
expect(isDefined(void 0)).to.be.false;
Expand All @@ -79,6 +80,7 @@ describe('lang', function() {

expect(isUndefined(null)).to.be.false;

// @ts-ignore-error "missing arg"
expect(isUndefined()).to.be.true;
expect(isUndefined(undefined)).to.be.true;
expect(isUndefined(void 0)).to.be.true;
Expand All @@ -98,6 +100,8 @@ describe('lang', function() {
expect(isNil({})).to.be.false;

expect(isNil(null)).to.be.true;

// @ts-ignore-error "missing arg"
expect(isNil()).to.be.true;
expect(isNil(undefined)).to.be.true;
expect(isNil(void 0)).to.be.true;
Expand Down

0 comments on commit e13dc95

Please sign in to comment.