rbx-is-type
TypeScript icon, indicating that this package has built-in type declarations

0.0.2 • Public • Published

is-type

This library comes with two functions:

interface IsACheckableTypes extends CheckableTypes, Instances {}
 
/**
 * Matches a given value to a given string type.
 * @param type A string which will match to a typeof call, or will match an Object's ClassName
 * @param value The value to match to a given type
 */
export declare function is<T extends keyof IsACheckableTypes>(type: T, value: any): value is IsACheckableTypes[T];
 
/**
 * Matches a given value to a given string type.
 * @param type A string which will match to a typeof call, or will match an Object:IsA(type) call
 * @param value The value to match to a given type
 */
export declare function isA<T extends keyof IsACheckableTypes>(type: T, value: any): value is IsACheckableTypes[T];

The implementations are pretty obvious:

is = function(typeStr, obj)
    local objType = typeof(obj);
    return objType == typeStr or objType == "Instance" and obj.ClassName == typeStr;
end;
 
isA = function(typeStr, obj)
    local objType = typeof(obj);
    return objType == typeStr or objType == "Instance" and obj:IsA(typeStr);
end;

These are very useful for type narrowing:

function Get(data: unknown) {
    if (is("number", data)) {
        // data is a number
    } else if (is("RemoteEvent", data)) {
        // data is a RemoteEvent
    } else if (isA("BasePart", data)) {
        // data is a BasePart
    }
}

Package Sidebar

Install

npm i rbx-is-type

Weekly Downloads

0

Version

0.0.2

License

ISC

Unpacked Size

3.76 kB

Total Files

4

Last publish

Collaborators

  • validark