如何在 Typescript 中使用 Chai expect() 验证特定类型?
How to verify for a specific type using Chai expect() in Typescript?
我曾尝试在以前的帖子中寻找答案,但我没有找到答案,所以这里是:
我目前正在尝试使用 Typescript 中 Chai 的 expect() 来测试对象实例 属性 值的返回类型。到目前为止,我试过这个:
/* My type file */
export type customType = "one" | "two" | "three";
export default customType;
/* My test file */
import customType from '{path_to_customType}'; // 'customType' is declared but its value is never read.
const newObject = new Object({property1: value1}) //note: value1 is of type customType
describe("Class1 test", () => {
it('my tests', () => {
expect(newObject).to.have.property("property1").to.equal("value1"); //so far, this one works fine
expect(newObject).to.be.a('customType'); // not working
expect(newObject).to.be.an('customType'); // not working
expect(newObject).to.be.a(customType); // not working
expect(newObject).to.be.an(customType); // not working
expect(typeof newObject.getProperty1()).to.equal('customType'); // not working
expect(newObject).to.be.an.instanceof(customType); // not working
expect(newObject).to.be.an.instanceof('customType'); // not working
assert.typeOf(newObject.getProperty1(), 'customType'); // not working
assert.typeOf(newObject.getProperty1(), customType); // not working
});
});
如您所见,value1
应该是 customType
类型,但它给了我一个错误,因为 customType
从未被读取过。
如何验证我的值是否属于特定的自定义类型?
**注意:在我的对象定义中,属性 property1
指定为 customType
类型
*UPDATE 我刚刚注意到 value1
的返回类型是 'string' 好像该类型只是一个别名...我该如何测试那么呢?
TypeScript 的类型系统纯粹是一个 compile-time 特性。 运行 代码没有类型。可以用typeof
来测试values是否是boolean, number, string, object等,也可以测试一个Object是否是一个class.
的实例
然而,您的 customType
根本不是可以在 运行 时检查的东西。一旦代码被转换为 JavaScript 以便 运行,customType
就像任何其他字符串一样只是一个字符串。
我曾尝试在以前的帖子中寻找答案,但我没有找到答案,所以这里是:
我目前正在尝试使用 Typescript 中 Chai 的 expect() 来测试对象实例 属性 值的返回类型。到目前为止,我试过这个:
/* My type file */
export type customType = "one" | "two" | "three";
export default customType;
/* My test file */
import customType from '{path_to_customType}'; // 'customType' is declared but its value is never read.
const newObject = new Object({property1: value1}) //note: value1 is of type customType
describe("Class1 test", () => {
it('my tests', () => {
expect(newObject).to.have.property("property1").to.equal("value1"); //so far, this one works fine
expect(newObject).to.be.a('customType'); // not working
expect(newObject).to.be.an('customType'); // not working
expect(newObject).to.be.a(customType); // not working
expect(newObject).to.be.an(customType); // not working
expect(typeof newObject.getProperty1()).to.equal('customType'); // not working
expect(newObject).to.be.an.instanceof(customType); // not working
expect(newObject).to.be.an.instanceof('customType'); // not working
assert.typeOf(newObject.getProperty1(), 'customType'); // not working
assert.typeOf(newObject.getProperty1(), customType); // not working
});
});
如您所见,value1
应该是 customType
类型,但它给了我一个错误,因为 customType
从未被读取过。
如何验证我的值是否属于特定的自定义类型?
**注意:在我的对象定义中,属性 property1
指定为 customType
*UPDATE 我刚刚注意到 value1
的返回类型是 'string' 好像该类型只是一个别名...我该如何测试那么呢?
TypeScript 的类型系统纯粹是一个 compile-time 特性。 运行 代码没有类型。可以用typeof
来测试values是否是boolean, number, string, object等,也可以测试一个Object是否是一个class.
然而,您的 customType
根本不是可以在 运行 时检查的东西。一旦代码被转换为 JavaScript 以便 运行,customType
就像任何其他字符串一样只是一个字符串。