Typescript-isEmpty函数的通用类型保护

时间:2018-09-05 15:07:38

标签: typescript typescript-generics

在缩小类型方面,我无法正确实现泛型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 for string values

isEmpty for array values

isEmpty for object values

代码可以在这里看到:https://github.com/Hotell/rex-tils/pull/13/files#diff-a3cdcb321a05315fcfc3309031eab1d8R177

相关问题:Type Guard for empty object

3 个答案:

答案 0 :(得分:2)

处理此问题的一种方法是将空值检查(null'')与空值检查([]{} isDefined)分开。我倾向于为此使用两种类型的保护措施-isEmptytypeof

第一个可能看起来像这样。请注意function isDefined<T>(value: T | undefined | null): value is T { return (typeof value !== 'undefined') && (value !== null); } 检查-这也使其与未声明的变量一起使用。

namespace Empty {
  export type String = '';
  export type Object = Record<string, never>;
  export type Array = never[];
}

type Empty =
  | Empty.Array
  | Empty.Object
  | Empty.String;

function isEmpty<T extends string | any[] | object>(subject: T | Empty): subject is Bottom<T> {
  switch (typeof subject) {
    case 'object':
      return (Object.keys(subject).length === 0);
    case 'string':
      return (subject === '');
    default:
      return false;
  }
}

type Bottom<T> =
  T extends string
    ? Empty.String
    : T extends any[]
        ? Empty.Array
        : T extends object
            ? Empty.Object
            : never;

对于空值,可以使用以下模型。

declare const foo: 'hello' | Empty.String;
declare const bar: [number, number] | Empty.Array;
declare const baz: Window | Empty.Object;

if (isEmpty(foo) && isEmpty(bar) && isEmpty(baz)) {
  console.log(foo, bar, baz);
}

正确推断出最低值。

T

编辑:根据建议在Route::post('testa', 'HomeController@doTest')->name('postTest'); 上添加了约束。

答案 1 :(得分:0)

这里

if (isEmpty(countries) || !isNumber(countryId)) {

您有两个条件,其中只有一个是countries的类型保护,这就是countries的类型在if内不变的原因。

对于对象,{}不代表空对象。除nullundefined以外的任何内容都可以分配给类型为{}的变量。您可能要改用{ [prop: string]: never }{ [prop: string]: undefined }

答案 2 :(得分:0)

因此,经过几次Twitter讨论和长时间的SO / Github搜索之后,我得到了以下解决方案:

  • 首先在isEmpty中检查null / undefined并没有多大意义(尽管lodash.isEmpty处理,恕我直言,它做得太多而且不是很明确)
  • 因为{} | |之间基本上没有区别object | any[]类型防护变窄将无法按预期工作
  • 最终解决方案仅接受有效值作为参数进行检查-> js对象以及字符串和警卫返回never,因此匹配值将为never类型,因为无论如何,它对在if(isEmpty(value)){ ... }语句中执行任何进一步的登录,而不是终止程序或引发错误

这是最终的实现方式:

const isEmpty = <T extends string | object | any[]>(
  value: T
): value is never => {
  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 ${
      // tslint:disable-next-line:strict-type-predicates
      value === null ? 'null' : typeof value
    }`
  )
}

https://github.com/Hotell/rex-tils/pull/13/files#diff-68ff3b6b6a1354b7277dfc4b23d99901R50