I'm quite a newbie to TypeScript with a strong C# background.
I wonder what's the exact reasons that the type inference doesn't seem to work in the following situation in TypeScript but does in C#:
TypeScript:
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);
}
Is this an issue of the TS compiler's type inference algorithm (maybe it's not there yet?) or is there some fundamental reason which I am missing and makes this impossible in TS?