通用散布元组类型在引用特定元素之前不会引发错误

问题描述

有一些我不想输入的类型定义,所以这里是Playground link

在回答this question时,我遇到了一个奇怪的情况。

我需要声明元组中每个元素的类型。现在,此函数仅在我引用特定属性时才会显示错误(因为我断言了返回值),但是对于该函数的参数,lint并没有抱怨。

const withArgsArr = <T extends WithArgs<Args<unkNown>,string | number>[]>(...configs: T) => configs.map(withArgs) as { [i in keyof T]: GetWithArgs<T[i]> };

const shouldAlsoBeInvalid = withArgsArr(
    {
        args: { good: { value: ["bool",true] } },fn: ({ good,bad }) => {}
    },{
        args: { num: { value: ["number",5] } },fn: ({ num }) => {}
    }
); // no errors yet :(

const requiresBool = shouldAlsoBeInvalid[0].fn({ good: 'string' }); // TS2322: Type 'string' is not assignable to type 'boolean'.
const badDoesNotExist = shouldAlsoBeInvalid[0].fn({ bad: 'string' }); // TS2345: Argument of type '{ bad: string; }' is not assignable to parameter of type '{ good: boolean; }'.
const requiresNumberParameter = shouldAlsoBeInvalid[1].fn({ num: '1' }); // TS2322: Type 'string' is not assignable to type 'number'.

我分别知道如何正确断言类型:

const invalid = withArgs({
    args: { good: { value: ["bool",bad }) => {} // Property 'bad' does not exist on type '{ good: boolean; }'
});

我想知道是否有一种方法可以通用地断言withArgsArr的传入类型。像这样:

const withArgsArr = <T extends *an array of WithArgs elements,for which each are a valid WithArgs element*>(...configs: T) => configs.map(withArgs) as { [i in keyof T]: GetWithArgs<T[i]> };

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...