类型为非假,又名 truthy

Type for non-false, aka truthy

在 TypeScript 中,有 truthy 类型吗?

我有这个方法: Object.keys(lck.lockholders).长度; enqueue(k: any, obj?: any): void 顺便说一句,想想 TS 有一种方法可以检查空字符串 ''。 我想将其转换为:

  enqueue(k: Truthy, obj?: any): void

除非我不知道如何定义 Truthy 的类型。顺便说一下,我 认为 TS 有一种检查空字符串 '' 的方法。

我想要这个的原因是我不希望用户传入 nullundefined'' 等作为散列的键。

我不确定你为什么需要这个,但它很有趣。老实说,简短的回答是:TypeScript 不适合这个,你最好进行运行时检查和记录你的代码,以便开发人员知道 k 参数应该是真实的。不过,如果您打算强制 TypeScript 做这样的事情,请继续阅读:


注意:要使以下内容正常工作,请打开 strictNullChecks 编译器选项。这有点必要,因为无法区分 TruthyTruthy | null | undefined 会是个问题。

你可以几乎定义falsy,就像

type Falsy = false | 0 | "" | null | undefined 

除了 NaN 也是假的,并且 TypeScript 没有 NaN 的数字文字(参见 microsoft/TypeScript#15135)。

即使你有上面的Falsy,TypeScript 中也没有否定类型(参见microsoft/TypeScript#4196),所以无法表达Truthy 作为“除 Falsy 之外的所有内容”。

您可以尝试使用 conditional types 来排除 enqueue() 中可能为假的参数,但这很奇怪:

type DefinitelyTruthy<T> =
  false extends T ? never :
  0 extends T ? never :
  "" extends T ? never :
  null extends T ? never :
  undefined extends T ? never :
  T

declare function enqueue<T extends number | string | true | object>(
  k: T & DefinitelyTruthy<T>,
  obj?: any
): void

declare const str: string;
enqueue(str); // error, might be falsy
enqueue("a"); // okay
enqueue(1); // okay
enqueue(0); // error
enqueue(NaN); // error
enqueue(true); // okay
enqueue(false); // error
enqueue([]); //okay
enqueue({a: "hello"}); // okay
enqueue({}); // error, interpreted as type {} which could be an empty string:
const zilch = "" as {};
enqueue(zilch); // error, see? 

请注意它不允许任何它认为可能是虚假的东西,这可能是您想要实现的。说不出来。


更新

我看到您编辑了问题以阐明 k 参数实际上应该是 string(或者可能是 symbol),并且您需要排除的唯一值是空字符串 ""。在这种情况下,您可以将上面的内容简化为:

type DefinitelyNotEmptyString<T> = "" extends T ? never : T

declare function enqueue<T extends string | symbol>(
  k: T & DefinitelyNotEmptyString<T>,
  obj?: any
): void

enqueue(""); // error
enqueue("a"); // okay

所有这一切都很棒,但不幸的是,如果您将通用的 string 传递给 enqueue(),它会失败,有时开发人员可能需要这样做,如果他们的价值正在使用的 k 参数不是他们指定的字符串文字:

declare const str: string; // comes from somewhere else
enqueue(str); // error!  how do I do this?

为了解决这个问题,您可以尝试创建一个 nominal type which you can use to identify to the compiler that a value has been checked for emptiness, and then make a user-defined type guard 来将 string 限制为该类型:

type NotEmptyString = string & {"***NotEmptyString***": true};
function notEmptyString(x: string): x is NotEmptyString {
  return x !== "";
}

现在开发者可以这样做:

declare const str: string;
enqueue(str); // error, might be falsy
if (notEmptyString(str)) {
  enqueue(str); // okay, str is NotEmptyString
}

哇!这是很多跳圈。如果您认为这是值得的,则取决于您。好的,希望有帮助。祝你好运!

没有 Truthy 类型,但是 你可以利用类型保护系统 (type predicate) 来帮助 TypeScript 理解什么是 [=13] =] 并赋值 truthy type!

让我们定义 Falsy 类型和 generic isTruthy 函数:

type Falsy = false | 0 | '' | null | undefined;

// this is a type predicate - if x is `truthy`, then it's T
const isTruthy = <T>(x: T | Falsy): x is T => !!x;

现在我们可以使用 isTruthy 函数来查找 Truthy 值,TypeScript 会正确地将 "truthy" 类型分配给结果。

示例:

{   // removing boolean "false" type
  const result: string | false = Math.random() > 0.5 ? 'a' : false;
  const truthyResult: string = isTruthy(result) ? result : 'was false';
}
{   // filtering only numbers from array
  const result: (number | undefined)[] = [42, undefined, 8, 16];
  const truthyResult: number[] = result.filter(isTruthy);
}
{   // filtering only Promises from array
  const result: (Promise<string> | null)[] = [Promise.resolve('a'), null, Promise.resolve('b'), Promise.resolve('c')];
  const truthyResult: Promise<string>[] = result.filter(isTruthy);
}