最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Typescript: How to check interface type - Stack Overflow

programmeradmin2浏览0评论

I have a method getUniqueId which accepts two kinds of interfaces below, which will return the uniqueId depending which interface type passed:

interface ReadOnlyInfo {
    itemId: string;
    ....
}

interface EditInfo {
    formId: number; 
    ....
}

function getUniqueId (info: ReadOnlyInfo | EditInfo) {
   if (info instanceof ReadOnlyInfo) {
       return info.itemId;
   }
   return info.formId;
}

I am wondering if this is a good practice to use instanceof here. My concern is that I might have many other methods similar to getUniqueId, which accept "ReadOnlyInfo | EditInfo" type as well, so I have to repeat "ReadOnlyInfo | EditInfo" everywhere.

Instead of doing that, I also tried using type:

type CommonInfo = | ReadOnlyInfo | EditInfo;

so, that I can save some code (just do CommondInfo), like below, but that way, I cannot tell which type CommonInfo is, instanceof no longer works and gives me compiler errors.

function getUniqueId (info: CommonInfo) {
     if (info instanceof ReadOnlyInfo) {
           return info.itemId;
       }
       return info.formId;   
}

So, I am wondering what would be the best practice to design interface/methods in this scenario. Thanks in advance!

I have a method getUniqueId which accepts two kinds of interfaces below, which will return the uniqueId depending which interface type passed:

interface ReadOnlyInfo {
    itemId: string;
    ....
}

interface EditInfo {
    formId: number; 
    ....
}

function getUniqueId (info: ReadOnlyInfo | EditInfo) {
   if (info instanceof ReadOnlyInfo) {
       return info.itemId;
   }
   return info.formId;
}

I am wondering if this is a good practice to use instanceof here. My concern is that I might have many other methods similar to getUniqueId, which accept "ReadOnlyInfo | EditInfo" type as well, so I have to repeat "ReadOnlyInfo | EditInfo" everywhere.

Instead of doing that, I also tried using type:

type CommonInfo = | ReadOnlyInfo | EditInfo;

so, that I can save some code (just do CommondInfo), like below, but that way, I cannot tell which type CommonInfo is, instanceof no longer works and gives me compiler errors.

function getUniqueId (info: CommonInfo) {
     if (info instanceof ReadOnlyInfo) {
           return info.itemId;
       }
       return info.formId;   
}

So, I am wondering what would be the best practice to design interface/methods in this scenario. Thanks in advance!

Share Improve this question asked Jul 29, 2022 at 6:17 flyingbeeflyingbee 6213 gold badges11 silver badges27 bronze badges 0
Add a comment  | 

2 Answers 2

Reset to default 14

Interfaces in Typescript just syntatic sugar for your ide. It is not possible to check interface types like in other languages. Knowing in C# for example.

But with type guard you can recognize which interface are in use.

interface ReadOnlyInfo {
    itemId: string;
    ....
}

interface EditInfo {
    formId: number; 
    ....
}

function getUniqueId (info: ReadOnlyInfo | EditInfo) {
    if (isReadOnlyInfo(info)) return info.itemId;
    if (isEditInfo(info)) return info.formId;
}

function isReadOnlyInfo(item: any): item is ReadOnlyInfo {
    return 'itemId' in item;
}

function isEditInfo(item: any): item is EditInfo {
    return 'formId' in item;
}

On this way you get a better ide support

You could use pattern where you use some common internal property across the interfaces which you want to differentiate . Ex.

export default User;

interface ReadOnlyInfo {
  _kind: 'readonly';
  itemId: string;
  ....
}

interface EditInfo {
  _kind: 'edit';
  formId: number; 
  ....
}

function getUniqueId (info: ReadOnlyInfo | EditInfo) {
 if (info._kind === 'readonly') {
     return info.itemId;
 }
 return info.formId;
}
发布评论

评论列表(0)

  1. 暂无评论