TypeScript type inference issue

后端 未结 2 527
一生所求
一生所求 2021-01-13 08:10

I\'m using TypeScript with the MongoDB node.js driver. Note, this is not a Mongo question, its just the particular use case of this issue I\'m having.

Pretty much ev

2条回答
  •  -上瘾入骨i
    2021-01-13 08:13

    I was wrong, this doesn't work in C# either. Here's the example of the same code from TypeScript, written in C#, that does not work:

    using System.IO;
    using System;
    using System.Threading.Tasks;
    
    class Program
    {
        static void Main()
        {
            Console.WriteLine("Hello, World!");
            var task = Wrapper(cb => FakeMongoFunctionWithCallback(5, cb));
        }
    
        static void FakeMongoFunctionWithCallback(int arg, Action callback)
        {
            // just pass through the arg into the callback, pretending we went to a db and came back with that result
            callback(null, arg);
        }
    
        static Task Wrapper(Action> action) {
            var tcs = new TaskCompletionSource();
            action((err, res) => {
                if (err != null) tcs.SetException(err);
                else tcs.SetResult(res);
            });
    
            return tcs.Task;
        } 
    }
    

提交回复
热议问题