如何对then()的参数进行类型检查

时间:2018-04-13 10:56:45

标签: typescript typescript2.0

在下面的函数中,Promise解析为typeof astring === 'number',因此strictFunctionTypes。但是,对于number,我没有收到astring: stringType '{}' is not assignable to type 'string'冲突的警告,而是看到then()

如何改进为function test(): Promise<string> { return new Promise((resolve, reject) => { resolve(1); }) .then((astring: string) => { return 'string'; }) } 提供的功能的类型检查?

#include <systemc>
#include <pthread.h>
#include <unistd.h>

using namespace sc_core;

class ThreadSafeEventIf : public sc_interface {
    virtual void notify(sc_time delay = SC_ZERO_TIME) = 0;
    virtual const sc_event &default_event(void) const = 0;
protected:
    virtual void update(void) = 0;
};

class ThreadSafeEvent : public sc_prim_channel, public ThreadSafeEventIf {
public:
    ThreadSafeEvent(const char *name = ""): event(name) {}

    void notify(sc_time delay = SC_ZERO_TIME) {
        this->delay = delay;
        async_request_update();
    }

    const sc_event &default_event(void) const {
        return event;
    }
protected:
    virtual void update(void) {
        event.notify(delay);
    }
    sc_event event;
    sc_time delay;
};

SC_MODULE(Foo)
{
public:
    SC_CTOR(Foo)
    {
        SC_THREAD(main);

        SC_METHOD(eventTriggered);
        sensitive << event;
        dont_initialize();
    }
private:
    void main() {
        usleep(5 * 1000 * 1000); // Just for the example, event is added to pending events during this sleep
        wait(SC_ZERO_TIME); // Schedule (event is evaluated here)
        usleep(1 * 1000 * 1000); // Just for the example
        std::cout << "Done" << std::endl;
    }

    void eventTriggered() {
        std::cout << "Got event" << std::endl;
    }

public:
    ThreadSafeEvent event;
};

void *externalHostThread(void *arg) {
    usleep(2 * 1000 * 1000); // Just for the example
    Foo* foo = (Foo*)(arg);
    foo->event.notify();
    std::cout << "Event notified from an external host thread" << std::endl;
}

int sc_main(int argc, char *argv[])
{
    Foo foo("foo");

    pthread_t thread;
    pthread_create(&thread, NULL, externalHostThread, &foo);

    sc_start();

    return 0;
}

1 个答案:

答案 0 :(得分:3)

您需要将类型参数指定为Promise,Typescript不会根据resolve的用法推断出承诺的返回类型为number

function test(): Promise<string> {
    return new Promise<number>((resolve, reject) => {
        resolve(1);
    })
    .then((nr: number) => {
        return 'string';
    })
}

//error
function test2(): Promise<string> {
    return new Promise<number>((resolve, reject) => {
        resolve(1);
    })
    .then((nr: string) => {
        return 'string';
    })
}