我有一个TypeScript函数,它返回Foo类型
interface Foo {
bar: string;
baz: string;
}
function getFoo(): Foo {
return {
bar: 'hello',
baz: 'world',
};
}
// Chai Assertion
it('Should return a Foo', () => {
expect(getFoo()).to.deep.equal({
bar: 'hello',
baz: 'world',
});
})当我更改Foo接口时,getFoo()函数会产生一个TypeScript错误:
interface Foo {
bar: number; // change these to numbers instead
baz: number;
}
function getFoo(): Foo {
// Compile time error! Numbers aren't strings!
return {
bar: 'hello',
baz: 'world',
};
}但是,我的Mocha测试不会触发编译时错误!
有一种类型安全的方法来做expect().to.deep.equal()吗?类似于:
// Strawman for how I'd like to do type-safety for deep equality assertions,
// though this generic signature still seems unnecessary?
expect<Foo>(getFoo()).to.deep.equal({
bar: 'hello',
baz: 'world',
});发布于 2019-01-15 23:16:26
是否有一种类型安全的方法来执行expect().to.deep.equal()
不是在equal的类型定义中,因为它是为运行时检查而设计的,因此有意使用any。
无论在外部做得多么容易:
const expected: Foo = {
bar: 'hello',
baz: 'world',
};
expect(getFoo()).to.deep.equal(expected);https://stackoverflow.com/questions/54207065
复制相似问题