是否在泛型方法中无法推断约束类型? [英] TypeScript cannot infer constrained type in generic method?

查看:0
本文介绍了是否在泛型方法中无法推断约束类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个打字新手,有很强的C#背景。

我想知道类型推断在以下情况下似乎不起作用,但在C#中却起作用的确切原因是什么:

打字稿:

interface IResult { }

interface IRequest<TResult extends IResult> { }

interface ISomeResult extends IResult {
    prop: string;
}

interface ISomeRequest extends IRequest<ISomeResult> { }

 function Process<TResult extends IResult>(request: IRequest<TResult>): TResult {
    throw "Not implemented";
}

let request: ISomeRequest = {};
let result = Process(request);
// Error: Property 'prop' does not exist on type '{}'.
console.log(result.prop);

C#

interface IResult { }

interface IRequest<TResult> where TResult : IResult { }

interface ISomeResult : IResult
{
    string Prop { get; set; }
}

interface ISomeRequest : IRequest<ISomeResult> { }

static TResult Process<TResult>(IRequest<TResult> request) where TResult : IResult
{
    throw new NotImplementedException();
}

static void Main()
{
    ISomeRequest request = default;
    var result = Process(request);
    // OK
    Console.WriteLine(result.Prop);
}

这是不是TS编译器的类型推断算法的问题(可能还不存在?)或者,有没有什么根本的原因让我在TS中无法做到这一点?

推荐答案

TypeScrip有一个结构类型系统(这在来自C#时可能看起来有点奇怪,我知道我也有同样的问题😊)。这意味着您有一个未使用的类型参数,它将被编译器忽略,因为它对类型兼容性并不重要。将使用TResult的任何成员添加到IRequest,它将按预期工作:

interface IResult { }

interface IRequest<TResult extends IResult> { 
    _r?: undefined | TResult
}

interface ISomeResult extends IResult {
    prop: string;
}

interface ISomeRequest extends IRequest<ISomeResult> { }

function Process<TResult extends IResult>(request: IRequest<TResult>): TResult {
    throw "Not implemented";
}

let request: ISomeRequest = { };
let result = Process(request);
console.log(result.prop);

FAQ对此进行一些说明。

这篇关于是否在泛型方法中无法推断约束类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆