简体   繁体   English

使用打字稿泛型检查对象是否为空

[英]Check if Object is Empty using typescript generic

I am new to typescript and in a learning phase.我是打字稿的新手并且处于学习阶段。 I am trying to create a generic to enforce the following condition我正在尝试创建一个泛型来强制执行以下条件

Consider I have a empty object考虑我有一个空对象

const data = {}

I need to create a generic which will check the following conditions我需要创建一个泛型来检查以下条件

Check if its an object if yes, then check if there is any data inside it , else return false如果是,检查它是否是一个对象,然后检查其中是否有任何数据,否则返回 false

Thanks in advance提前致谢

You can use this utility to check whether object type is empty or not:您可以使用此实用程序来检查对象类型是否为空:

// credits goes to https://github.com/microsoft/TypeScript/issues/23182#issuecomment-379091887
type IsEmptyObject<Obj extends Record<PropertyKey, unknown>> =
    [keyof Obj] extends [never] ? true : false

type Test = IsEmptyObject<{}> // true

type Test2 = IsEmptyObject<{ age: 42 }> // false

If Obj does not have any keys then keyof Obj returns never .如果Obj没有任何键,则keyof Obj返回never

Our goal is to check whether it returns never or not.我们的目标是检查它是否never返回。

In order to check it, we need to stop distributivity .为了检查它,我们需要停止分配

If keyof Obj returns never then our conditional type IsEmptyObject returns true .如果keyof Obj返回never则我们的条件类型IsEmptyObject返回true

If you want to use it in a function, consider this example:如果要在函数中使用它,请考虑以下示例:


type IsEmptyObject<Obj extends Record<PropertyKey, unknown>> =
    [keyof Obj] extends [never] ? true : false

function isEmpty<Obj extends Record<PropertyKey, unknown>>(obj: Obj): IsEmptyObject<Obj>
function isEmpty<Obj extends Record<PropertyKey, unknown>>(obj: Obj) {
    return Object.keys(obj).length === 0
}

const result = isEmpty({}) // true
const result2 = isEmpty({ age: 42 }) // false

Playground 操场

Also you need to be aware that it works only with literal types.您还需要注意它仅适用于文字类型。

If you want to make it work with higher order function and I bet you want, please consider this example:如果你想让它与高阶函数一起工作并且我打赌你想要,请考虑这个例子:

type IsEmptyObject<Obj extends Record<PropertyKey, unknown>, Keys = keyof Obj> =
    PropertyKey extends Keys ? boolean : [keyof Obj] extends [never] ? true : false

function isEmpty<Obj extends Record<PropertyKey, unknown>>(obj: Obj): IsEmptyObject<Obj>
function isEmpty<Obj extends Record<PropertyKey, unknown>>(obj: Obj) {
    return Object.keys(obj).length === 0
}

const higherOrderFunction = (obj: Record<PropertyKey, unknown>) => {
    const test = isEmpty(obj) // boolean

    return test
}

const result3 = higherOrderFunction({ age: 2 }) // boolean

If isEmpty is unable to infer literal type, it will return boolean by the default, since you never know what you can get in higher order function.如果isEmpty无法推断文字类型,则默认情况下它将返回boolean值,因为您永远不知道在高阶函数中可以得到什么。

If you infer obj argument in higherOrderFunction , isEmpty in turn will be able to infer argument too.如果您在higherOrderFunction推断obj参数,则isEmpty反过来也能够推断出参数。

const higherOrderFunction = <T extends Record<PropertyKey, unknown>>(obj: T) => {
    const test = isEmpty(obj) // IsEmptyObject<T, keyof T>

    return test
}

const result3 = higherOrderFunction({ age: 2 }) // false
const result4 = higherOrderFunction({ }) // true

Playground 操场

I'm not sure what you mean: then check if there is any data inside it .我不确定你的意思: then check if there is any data inside it

Question:题:

Let's say we have this object: {age: undefined} .假设我们有这个对象: {age: undefined} Do you consider it with data or not ?您是否考虑使用数据?

const emptyObject = (data:Object) => {
    if(typeof data == "object"){
        if(Object.keys(data).length == 0){
            console.log("Empty Object");
            return true;
        }
        else{
            console.log("Not Empty Object");
        }
    }
    else{
        console.log("Not an Object");
    }
    return false;
} 

Here are some examples and the logs generated.以下是一些示例和生成的日志。

EXAMPLES例子

console.log(emptyObject({}));
console.log(emptyObject("acd"));
console.log(emptyObject({
    "acd": 1
}));

LOGS日志

[LOG]: "Empty Object" 
[LOG]: true 
[LOG]: "Not an Object" 
[LOG]: false 
[LOG]: "Not Empty Object" 
[LOG]: false

 const data = {}; console.log("Check if object is empty", Object.keys(data).length == 0); console.log("Check if it is object", typeof data === "object");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM