Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
typescript条件类型:推理有效,但编译器拒绝实现_Typescript_Conditional Types - Fatal编程技术网

typescript条件类型:推理有效,但编译器拒绝实现

typescript条件类型:推理有效,但编译器拒绝实现,typescript,conditional-types,Typescript,Conditional Types,我已经非常成功地使用了typescript的条件类型,但我经常发现,虽然我可以表达类型签名,但在函数的实现中,我必须使用任何类型,即使我知道实现是正确的 例如,此定义: type ExportType<InputType extends string|undefined> = string extends InputType ? undefined : ()=>void; function convert<InputType extends string|und

我已经非常成功地使用了typescript的条件类型,但我经常发现,虽然我可以表达类型签名,但在函数的实现中,我必须使用任何类型,即使我知道实现是正确的

例如,此定义:

type ExportType<InputType extends string|undefined> =
    string extends InputType ? undefined : ()=>void;

function convert<InputType extends string|undefined>(input: InputType): ExportType<InputType>;
这是不可编译的。打字稿上写着:

error TS2322: Type '() => void' is not assignable to type 'ExportType<InputType>'.

         return ()=>{console.log("OK");};
         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
使它工作,但它令人失望


那么,有没有一种方法可以为typescript允许我们表达的签名编写一个完全类型安全且经过检查的实现?

typescript将不允许您为未解析的条件类型(即仍然包含自由类型参数的类型)赋值。您可以使用发现的强制转换,也可以使用单独的实现签名:

type ExportType<InputType extends string|undefined> =
    string extends InputType ? undefined : ()=>void;

function convert<InputType extends string|undefined>(input: InputType): ExportType<InputType> 
function convert(input: string|undefined): undefined | (()=>void) {
    if (input) {
        return ()=>{console.log("OK");};
    } else {
        return undefined;
    }
}

那真令人失望。。我感觉高级语言功能只针对包装JS库进行了调整,因此,.d.ts操作模式和实现本身并不是真正的焦点:-你认为这是暂时的情况,还是不太可能改变?@EmmanuelTouzery我认为这不太公平。。。高级功能在实现中可用。。只是不是这个特殊情况。一般来说,typescript不允许将值分配给泛型类型,而条件类型甚至比这更进一步。我同意你的看法,他们会投入更多资金,让这些通用函数的调用站点比实现更好地工作。一般来说,这种类型的函数只编写一次,并使用多次,因此在实现中使用所有类型时投入较少的资金是有意义的
    return <any>(()=>{console.log("OK");});
type ExportType<InputType extends string|undefined> =
    string extends InputType ? undefined : ()=>void;

function convert<InputType extends string|undefined>(input: InputType): ExportType<InputType> 
function convert(input: string|undefined): undefined | (()=>void) {
    if (input) {
        return ()=>{console.log("OK");};
    } else {
        return undefined;
    }
}