首页 > 解决方案 > TypeScript:在此示例中预期类型错误。而没有得到他们。我错过了什么吗?还是只是丢了??:)

问题描述

对 TypeScript 来说不是那么新,但也许足够新?我在这里很困惑..我期待一些编译器错误并且没有得到它们。我错过了什么吗?

(更好/可运行的示例在 TS Playground:Playground Link上)

type Context<T> = {
    data:any,
};

type EventNames = "beforeSave" | "afterSave" | "beforeDelete" | "afterDelete";


type EventHandler<T> = (context:Context<T>, next:()=>{})=>Promise<any>;

type EntityModel<T> = {
    observe:(eventName:EventNames, handler:EventHandler<T>)=>void;
}

type OnlineShoppingCart ={
    [key: string]:any
}


const model:EntityModel<OnlineShoppingCart> = {
    observe: (eventName:EventNames)=> { 
// why am I not getting a compiler error here? 
// observe is not properly implemented!

    }
};

model.observe('beforeSave', async ()=>{
// why am I not getting a compiler error here? 
//This arrow function does not properly implement EventHandler<T>
})

标签: node.jsangulartypescripttypestypescript-generics

解决方案


推荐阅读