Typescript接口:为什么Typescript允许不兼容的接口

时间:2016-09-15 03:11:27

标签: javascript typescript

所以,我对Typescript接口有点困惑。最好在实际代码中显示

interface Schedule {
    scheduleId: number
    projectId: number
    scheduleVersions: ScheduleVersions[]
}

interface ScheduleVersions {
    created: string
}

function format(b: Schedule) {
  let id = b.projectId 
  let scheduleId = b.scheduleId
    return b.scheduleVersions[0].created + String(id) + String(scheduleId)
}

interface IPromise <T> {
    then <U> ( f: (a:T) => U ) : IPromise<U>
}

//missing properties 
var c : IPromise<{ scheduleId: number }>
// here it should error, isn't it? because format function expect Schedule in it argument
// and type inside IPromise have 2 missing properties.
c.then(format)

那是什么,为什么打字稿不会给我一个错误?告诉我IPromise中的类型与Schedule不兼容。

0 个答案:

没有答案