打字稿:检查确切的类型

时间:2019-03-15 11:38:52

标签: typescript types typechecking

我是Typescript的新手,我想知道是否可以做这样的事情:我的项目中有旧版代码(API),其中某些端点返回相同的命名键(例如idUser),但是它们具有不同的含义(一种情况是客户ID,另一种是经理ID)。所以我想在使用时检查类型:

   type idManager = number;
   type idClient = number;

   interface IManager {
       userId: idManager
   }

   interface IClient {
       userId: idClient 
   }

   const mid: idManager = 23;
   const cid: idClient = 437;

   // type check passed
   const manager: IManager = {
       userId: mid
   }

   // I want TS to throw type error, because its not idClient,
   // but type check is passing, because its just a number
   const client: IClient = {
       userId: mid
   }

0 个答案:

没有答案