Я не могу правильно реализовать общий isEmpty(value)
с точки зрения ограничения типа сужения предоставленного значения до его пустого аналога.
Пример использования:
function getCountryNameById(countries: LookupItem[] = [], countryId?: number): string | undefined {
if (isEmpty(countries) || !isNumber(countryId)) {
// within this branch I would like to get countries argument to be narrowed to empty array type.
// Same would apply for other function which can have argument type of object or string. Why ? -> to prevent someone to do some mad code hacks like accessing non existent value from empty array ( which would happen on runtime ofc ) on compile time
// $ExpectType []
console.log(countries)
return
}
// continue with code logic ...
// implementation ...
}
аналогичный случай на ограничивающем объекте:
function doSomethingWithObject( data: { foo: string; bar: number } | object ){
if(isEmpty(data)){
// $ExpectType {}
data
// following should throw compile error, as data is empty object
data.foo.toUpercase()
return
}
// here we are sure that data is not empty on both runtime and compile time
}
Реализация защиты типа isEmpty:
export const isEmpty = <T extends AllowedEmptyCheckTypes>(
value: T | AllowedEmptyCheckTypes
): value is Empty<T> => {
if (isBlank(value)) {
return true
}
if (isString(value) || isArray(value)) {
return value.length === 0
}
if (isObject(value)) {
return Object.keys(value).length === 0
}
throw new Error(
`checked value must be type of string | array | object. You provided ${typeof value}`
)
}
С определенными типами:
type EmptyArray = Array<never>
type Blank = null | undefined | void
/**
* // object collects {} and Array<any> so adding both {} and Array<any> is not needed
* @private
*/
export type AllowedEmptyCheckTypes = Blank | string | object
/**
* Empty mapped type that will cast any AllowedEmptyCheckTypes to empty equivalent
* @private
*/
export type Empty<T extends AllowedEmptyCheckTypes> = T extends string
? ''
: T extends any[]
? EmptyArray
: T extends object ? {} : T extends Blank ? T : never
Это как-то странно, поскольку с точки зрения типов он сужается правильно, но не внутри ветки if / else:
isEmpty для строковых значений
код можно увидеть здесь: https://github.com/Hotell/rex-tils/pull/13/files#diff-a3cdcb321a05315fcfc3309031eab1d8R177
Связанный вопрос: Type Guard для пустого объекта