Improve TS code by adding generics and removing any where applicable (#49)
This commit is contained in:
parent
55c00956f9
commit
1df2ff09ce
3 changed files with 42 additions and 45 deletions
|
|
@ -8,6 +8,13 @@ export interface ArrayLike {
|
||||||
length: number;
|
length: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface Class<T = any> {
|
||||||
|
new(...args: any[]): T;
|
||||||
|
}
|
||||||
|
|
||||||
|
type DomElement = object & { nodeType: 1; nodeName: string };
|
||||||
|
type NodeStream = object & { pipe: Function };
|
||||||
|
|
||||||
export const enum TypeName {
|
export const enum TypeName {
|
||||||
null = 'null',
|
null = 'null',
|
||||||
boolean = 'boolean',
|
boolean = 'boolean',
|
||||||
|
|
@ -60,30 +67,25 @@ const getObjectType = (value: any): TypeName | null => {
|
||||||
const isObjectOfType = <T>(type: TypeName) => (value: any): value is T => getObjectType(value) === type;
|
const isObjectOfType = <T>(type: TypeName) => (value: any): value is T => getObjectType(value) === type;
|
||||||
|
|
||||||
function is(value: any): TypeName { // tslint:disable-line:only-arrow-functions
|
function is(value: any): TypeName { // tslint:disable-line:only-arrow-functions
|
||||||
if (value === null) {
|
switch (value) {
|
||||||
return TypeName.null;
|
case null:
|
||||||
|
return TypeName.null;
|
||||||
|
case true:
|
||||||
|
case false:
|
||||||
|
return TypeName.boolean;
|
||||||
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
if (value === true || value === false) {
|
switch (typeof value) {
|
||||||
return TypeName.boolean;
|
case 'undefined':
|
||||||
}
|
return TypeName.undefined;
|
||||||
|
case 'string':
|
||||||
const type = typeof value;
|
return TypeName.string;
|
||||||
|
case 'number':
|
||||||
if (type === 'undefined') {
|
return TypeName.number;
|
||||||
return TypeName.undefined;
|
case 'symbol':
|
||||||
}
|
return TypeName.symbol;
|
||||||
|
default:
|
||||||
if (type === 'string') {
|
|
||||||
return TypeName.string;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (type === 'number') {
|
|
||||||
return TypeName.number;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (type === 'symbol') {
|
|
||||||
return TypeName.symbol;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is.function_(value)) {
|
if (is.function_(value)) {
|
||||||
|
|
@ -115,7 +117,7 @@ function is(value: any): TypeName { // tslint:disable-line:only-arrow-functions
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace is { // tslint:disable-line:no-namespace
|
namespace is { // tslint:disable-line:no-namespace
|
||||||
const isObject = (value: any) => typeof value === 'object';
|
const isObject = (value: any): value is object => typeof value === 'object';
|
||||||
|
|
||||||
// tslint:disable:variable-name
|
// tslint:disable:variable-name
|
||||||
export const undefined = isOfType<undefined>('undefined');
|
export const undefined = isOfType<undefined>('undefined');
|
||||||
|
|
@ -123,7 +125,7 @@ namespace is { // tslint:disable-line:no-namespace
|
||||||
export const number = isOfType<number>('number');
|
export const number = isOfType<number>('number');
|
||||||
export const function_ = isOfType<Function>('function');
|
export const function_ = isOfType<Function>('function');
|
||||||
export const null_ = (value: any): value is null => value === null;
|
export const null_ = (value: any): value is null => value === null;
|
||||||
export const class_ = (value: any) => function_(value) && value.toString().startsWith('class ');
|
export const class_ = (value: any): value is Class => function_(value) && value.toString().startsWith('class ');
|
||||||
export const boolean = (value: any): value is boolean => value === true || value === false;
|
export const boolean = (value: any): value is boolean => value === true || value === false;
|
||||||
export const symbol = isOfType<Symbol>('symbol');
|
export const symbol = isOfType<Symbol>('symbol');
|
||||||
// tslint:enable:variable-name
|
// tslint:enable:variable-name
|
||||||
|
|
@ -132,32 +134,32 @@ namespace is { // tslint:disable-line:no-namespace
|
||||||
export const buffer = Buffer.isBuffer;
|
export const buffer = Buffer.isBuffer;
|
||||||
|
|
||||||
export const nullOrUndefined = (value: any): value is null | undefined => null_(value) || undefined(value);
|
export const nullOrUndefined = (value: any): value is null | undefined => null_(value) || undefined(value);
|
||||||
export const object = (value: any) => !nullOrUndefined(value) && (function_(value) || isObject(value));
|
export const object = (value: any): value is object => !nullOrUndefined(value) && (function_(value) || isObject(value));
|
||||||
export const iterable = (value: any): value is Iterator<any> => !nullOrUndefined(value) && function_(value[Symbol.iterator]);
|
export const iterable = (value: any): value is IterableIterator<any> => !nullOrUndefined(value) && function_(value[Symbol.iterator]);
|
||||||
export const generator = (value: any): value is Generator => iterable(value) && function_(value.next) && function_(value.throw);
|
export const generator = (value: any): value is Generator => iterable(value) && function_(value.next) && function_(value.throw);
|
||||||
|
|
||||||
export const nativePromise = isObjectOfType<Promise<any>>(TypeName.Promise);
|
export const nativePromise = (value: any): value is Promise<any> =>
|
||||||
|
isObjectOfType<Promise<any>>(TypeName.Promise)(value);
|
||||||
|
|
||||||
const hasPromiseAPI = (value: any): value is Promise<any> =>
|
const hasPromiseAPI = (value: any): value is Promise<any> =>
|
||||||
!null_(value) &&
|
!null_(value) &&
|
||||||
isObject(value) &&
|
isObject(value) as any &&
|
||||||
function_(value.then) &&
|
function_(value.then) &&
|
||||||
function_(value.catch);
|
function_(value.catch);
|
||||||
|
|
||||||
export const promise = (value: any): value is Promise<any> => nativePromise(value) || hasPromiseAPI(value);
|
export const promise = (value: any): value is Promise<any> => nativePromise(value) || hasPromiseAPI(value);
|
||||||
|
|
||||||
const isFunctionOfType = <T>(type: TypeName) => isObjectOfType<T>(type);
|
export const generatorFunction = isObjectOfType<GeneratorFunction>(TypeName.GeneratorFunction);
|
||||||
export const generatorFunction = isFunctionOfType<GeneratorFunction>(TypeName.GeneratorFunction);
|
export const asyncFunction = isObjectOfType<Function>(TypeName.AsyncFunction);
|
||||||
export const asyncFunction = isFunctionOfType<Function>(TypeName.AsyncFunction);
|
|
||||||
export const boundFunction = (value: any): value is Function => function_(value) && !value.hasOwnProperty('prototype');
|
export const boundFunction = (value: any): value is Function => function_(value) && !value.hasOwnProperty('prototype');
|
||||||
|
|
||||||
export const regExp = isObjectOfType<RegExp>(TypeName.RegExp);
|
export const regExp = isObjectOfType<RegExp>(TypeName.RegExp);
|
||||||
export const date = isObjectOfType<Date>(TypeName.Date);
|
export const date = isObjectOfType<Date>(TypeName.Date);
|
||||||
export const error = isObjectOfType<Error>(TypeName.Error);
|
export const error = isObjectOfType<Error>(TypeName.Error);
|
||||||
export const map = isObjectOfType<Map<any, any>>(TypeName.Map);
|
export const map = (value: any): value is Map<any, any> => isObjectOfType<Map<any, any>>(TypeName.Map)(value);
|
||||||
export const set = isObjectOfType<Set<any>>(TypeName.Set);
|
export const set = (value: any): value is Set<any> => isObjectOfType<Set<any>>(TypeName.Set)(value);
|
||||||
export const weakMap = isObjectOfType<WeakMap<any, any>>(TypeName.WeakMap);
|
export const weakMap = (value: any): value is WeakMap<any, any> => isObjectOfType<WeakMap<any, any>>(TypeName.WeakMap)(value);
|
||||||
export const weakSet = isObjectOfType<WeakSet<any>>(TypeName.WeakSet);
|
export const weakSet = (value: any): value is WeakSet<any> => isObjectOfType<WeakSet<any>>(TypeName.WeakSet)(value);
|
||||||
|
|
||||||
export const int8Array = isObjectOfType<Int8Array>(TypeName.Int8Array);
|
export const int8Array = isObjectOfType<Int8Array>(TypeName.Int8Array);
|
||||||
export const uint8Array = isObjectOfType<Uint8Array>(TypeName.Uint8Array);
|
export const uint8Array = isObjectOfType<Uint8Array>(TypeName.Uint8Array);
|
||||||
|
|
@ -173,7 +175,7 @@ namespace is { // tslint:disable-line:no-namespace
|
||||||
export const sharedArrayBuffer = isObjectOfType<SharedArrayBuffer>(TypeName.SharedArrayBuffer);
|
export const sharedArrayBuffer = isObjectOfType<SharedArrayBuffer>(TypeName.SharedArrayBuffer);
|
||||||
export const dataView = isObjectOfType<DataView>(TypeName.DataView);
|
export const dataView = isObjectOfType<DataView>(TypeName.DataView);
|
||||||
|
|
||||||
export const directInstanceOf = (instance: any, klass: any) => Object.getPrototypeOf(instance) === klass.prototype;
|
export const directInstanceOf = <T>(instance: any, klass: Class<T>): instance is T => Object.getPrototypeOf(instance) === klass.prototype;
|
||||||
|
|
||||||
export const truthy = (value: any) => Boolean(value);
|
export const truthy = (value: any) => Boolean(value);
|
||||||
export const falsy = (value: any) => !value;
|
export const falsy = (value: any) => !value;
|
||||||
|
|
@ -247,11 +249,11 @@ namespace is { // tslint:disable-line:no-namespace
|
||||||
'nodeValue'
|
'nodeValue'
|
||||||
];
|
];
|
||||||
|
|
||||||
export const domElement = (value: any) => object(value) && value.nodeType === NODE_TYPE_ELEMENT && string(value.nodeName) &&
|
export const domElement = (value: any): value is DomElement => object(value) as any && value.nodeType === NODE_TYPE_ELEMENT && string(value.nodeName) &&
|
||||||
!plainObject(value) && DOM_PROPERTIES_TO_CHECK.every(property => property in value);
|
!plainObject(value) && DOM_PROPERTIES_TO_CHECK.every(property => property in value);
|
||||||
|
|
||||||
export const observable = (value: any) => Boolean(value && value[symbolObservable] && value === value[symbolObservable]());
|
export const observable = (value: any) => Boolean(value && value[symbolObservable] && value === value[symbolObservable]());
|
||||||
export const nodeStream = (value: any) => !nullOrUndefined(value) && isObject(value) && function_(value.pipe) && !observable(value);
|
export const nodeStream = (value: any): value is NodeStream => !nullOrUndefined(value) && isObject(value) as any && function_(value.pipe) && !observable(value);
|
||||||
|
|
||||||
export const infinite = (value: any) => value === Infinity || value === -Infinity;
|
export const infinite = (value: any) => value === Infinity || value === -Infinity;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -332,7 +332,7 @@ const types = new Map<string, Test>([
|
||||||
]
|
]
|
||||||
}],
|
}],
|
||||||
['infinite', {
|
['infinite', {
|
||||||
is: m.infinite,
|
is: m.infinite,
|
||||||
fixtures: [
|
fixtures: [
|
||||||
Infinity,
|
Infinity,
|
||||||
-Infinity
|
-Infinity
|
||||||
|
|
|
||||||
|
|
@ -14,16 +14,11 @@
|
||||||
"es2017.sharedmemory"
|
"es2017.sharedmemory"
|
||||||
],
|
],
|
||||||
"stripInternal": true,
|
"stripInternal": true,
|
||||||
"noImplicitAny": true,
|
"strict": true,
|
||||||
"noImplicitReturns": true,
|
"noImplicitReturns": true,
|
||||||
"noImplicitThis": true,
|
|
||||||
"noUnusedLocals": true,
|
"noUnusedLocals": true,
|
||||||
"noUnusedParameters": true,
|
"noUnusedParameters": true,
|
||||||
"noFallthroughCasesInSwitch": true,
|
"noFallthroughCasesInSwitch": true,
|
||||||
"strictNullChecks": true,
|
|
||||||
"strictFunctionTypes": true,
|
|
||||||
"strictPropertyInitialization": true,
|
|
||||||
"alwaysStrict": true,
|
|
||||||
"esModuleInterop": true
|
"esModuleInterop": true
|
||||||
},
|
},
|
||||||
"exclude": [
|
"exclude": [
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue