0.0.2 • Published 5 years ago

rbx-is-type v0.0.2

Weekly downloads
2
License
ISC
Repository
github
Last release
5 years ago

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
	}
}