小编Geo*_*ids的帖子

RxJS 过滤函数不会缩小类型,除非直接给出类型保护,它是唯一的参数

我一直在开发一个身份验证服务,该服务使用 rxjs 行为主题来存储最后检索到的身份验证对象,并在该对象已过期(或根本尚未获取)时触发重新获取。

我的问题是关于 TypeScript 类型检查器。我已经编写了断言的类型保护程序isNotUndefined- 嗯,正是您所期望的。

export function isNotUndefined<T>(input: T | undefined): input is T {
  return input !== undefined;
}
Run Code Online (Sandbox Code Playgroud)

我已经不得不编写上面的 typeguard 而不是能够依赖auth !== undefined. 我现在无法理解为什么在authGetter$下面代码的管道中,管道中值的类型没有减少到Auth第一个过滤器之后。相反,类型仍然是Auth | undefined,并且需要第二个过滤器仅带有类型保护才能将类型缩小到仅Auth

总而言之,为什么我需要第二个过滤器将类型缩小到仅Auth?此外,因为我是自己编码,没有人审查它,所以我非常感谢任何人指出他们认识到的“代码味道”(并提供有关如何处理的建议)。

export default class AuthService {
  private static lastAuth$ = new BehaviorSubject<Auth | undefined>(undefined);

  private static authGetter$ = AuthService.lastAuth$.pipe(
    filter(auth => {
      if (isNotUndefined(auth) && auth.expiry > new Date()) {
        return true ; // identical resulting …
Run Code Online (Sandbox Code Playgroud)

authorization typechecking observable typescript typeguards

9
推荐指数
1
解决办法
4233
查看次数

如何将字符串类型数组转换为变量类型数组-Excel VBA

我试图将以制表符分隔的文本文件中存储的公式加载到工作表中的某个范围。我已经使用该函数Split(Expression As String, Delimiter)正确地将每一行依次加载到一维数组中,但是遇到了有关返回的数组类型的问题。

Split函数仅返回字符串类型的数组,而我需要一个变量类型数组来将范围设置为。这是因为使用字符串类型数组设置单元格的公式会导致将单元格值设置为原始文本,即使字符串以等号开头。

'Example code to demonstrate the problem:
Sub Tester()
    Dim StringArr(1 To 3) As String
    StringArr(1) = "= 1"
    StringArr(2) = "= 2"
    StringArr(3) = "= 3"
    Range("Sheet1!$A$1:$C$1").Formula = StringArr
    'Cells display raw string until edited manually

    Dim VariantArr(1 To 3) As Variant
    VariantArr(1) = "= 1"
    VariantArr(2) = "= 2"
    VariantArr(3) = "= 3"
    Range("Sheet1!$A$2:$C$2").Formula = VariantArr
    'Cells display formula result correctly
End Sub
Run Code Online (Sandbox Code Playgroud)

结果输出:

输出

我想知道是否有一种方法可以将Split函数返回的数组转换为变量类型数组,最好没有循环。我知道我可以在一个循环中分别设置每个单元格公式,但是我试图使其尽可能高效和整洁。

我正在使用Microsoft Excel for Mac 2011 …

arrays string variant excel-vba cells

5
推荐指数
1
解决办法
4527
查看次数

为什么不在 ts 严格模式下Parameters&lt;Func&gt;扩展unknown[] (Array&lt;unknown&gt;)

标题几乎说明了一切。我有这个代码:

  type testNoArgsF = () => number;
  type testArgsF = (arg1: boolean, arg2: string) => number;
  type unknownArgsF = (...args: unknown[]) => number;
  type anyArgsF = (...args: any[]) => number;

  type testII = testArgsF extends anyArgsF ? true : false; // true
  type testIII = Parameters<testArgsF> extends Parameters<unknownArgsF>
    ? true
    : false; // true

  // unexpected:
  type testIV = testArgsF extends unknownArgsF ? true : false; // false <- why?
  // even though:
  type testV = testNoArgsF extends unknownArgsF ? …
Run Code Online (Sandbox Code Playgroud)

extends types arguments typescript spread-syntax

1
推荐指数
1
解决办法
1587
查看次数