Narrow types for isOfType and consolidate typeof switch in is
This commit is contained in:
parent
30572a045d
commit
b909111f10
1 changed files with 8 additions and 12 deletions
|
|
@ -98,7 +98,8 @@ function isPrimitiveTypeName(name: any): name is PrimitiveTypeName {
|
|||
export type TypeName = ObjectTypeName | PrimitiveTypeName;
|
||||
|
||||
const {toString} = Object.prototype;
|
||||
const isOfType = <T>(type: string) => (value: unknown): value is T => typeof value === type;
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||
const isOfType = <T extends Function | Primitive>(type: string) => (value: unknown): value is T => typeof value === type;
|
||||
|
||||
const getObjectType = (value: unknown): ObjectTypeName | undefined => {
|
||||
const objectTypeName = toString.call(value).slice(8, -1);
|
||||
|
|
@ -117,13 +118,8 @@ const getObjectType = (value: unknown): ObjectTypeName | undefined => {
|
|||
const isObjectOfType = <T>(type: ObjectTypeName) => (value: unknown): value is T => getObjectType(value) === type;
|
||||
|
||||
function is(value: unknown): TypeName {
|
||||
switch (value) {
|
||||
case null:
|
||||
return 'null';
|
||||
case true:
|
||||
case false:
|
||||
return 'boolean';
|
||||
default:
|
||||
if (value === null) {
|
||||
return 'null';
|
||||
}
|
||||
|
||||
switch (typeof value) {
|
||||
|
|
@ -133,6 +129,10 @@ function is(value: unknown): TypeName {
|
|||
return 'string';
|
||||
case 'number':
|
||||
return 'number';
|
||||
case 'boolean':
|
||||
return 'boolean';
|
||||
case 'function':
|
||||
return 'Function';
|
||||
case 'bigint':
|
||||
return 'bigint';
|
||||
case 'symbol':
|
||||
|
|
@ -140,10 +140,6 @@ function is(value: unknown): TypeName {
|
|||
default:
|
||||
}
|
||||
|
||||
if (is.function_(value)) {
|
||||
return 'Function';
|
||||
}
|
||||
|
||||
if (is.observable(value)) {
|
||||
return 'Observable';
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue