The left-hand side of an ‘instanceof’ expression must be of type ‘any’, an object type or a type parameter

I’ve been playing around with TypeScript (I’m still learning) and writing up a few test scenarios to better understand programming fundamentals.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>export class Base {
readonly id: string;
constructor(id: string) {
this.id = id;
}
}
export class A extends Base {
image: string;
constructor(id: string, image: string) {
super(id);
this.image = image;
}
}
export class B extends Base {
occupancy: number;
constructor(id: string, occupancy: number) {
super(id);
this.occupancy = occupancy;
}
}
export class C extends Base {
noiseLevel: string;
constructor(id: string, noiseLevel: string) {
super(id);
this.noiseLevel = noiseLevel;
}
}
function doSomething(param: A | B | C): string {
if (param instanceof Base) {
return 'base';
} else if (param instanceof A) {
return 'a';
} else if (param instanceof B) {
return 'b';
} else if (param instanceof C) {
return 'c';
}
}
</code>
<code>export class Base { readonly id: string; constructor(id: string) { this.id = id; } } export class A extends Base { image: string; constructor(id: string, image: string) { super(id); this.image = image; } } export class B extends Base { occupancy: number; constructor(id: string, occupancy: number) { super(id); this.occupancy = occupancy; } } export class C extends Base { noiseLevel: string; constructor(id: string, noiseLevel: string) { super(id); this.noiseLevel = noiseLevel; } } function doSomething(param: A | B | C): string { if (param instanceof Base) { return 'base'; } else if (param instanceof A) { return 'a'; } else if (param instanceof B) { return 'b'; } else if (param instanceof C) { return 'c'; } } </code>
export class Base {
    readonly id: string;

    constructor(id: string) {
        this.id = id;
    }
}


export class A extends Base {
    image: string;

    constructor(id: string, image: string) {
        super(id);
        this.image = image;
    }
}

export class B extends Base {
    occupancy: number;

    constructor(id: string, occupancy: number) {
        super(id);
        this.occupancy = occupancy;
    }
}

export class C extends Base {
    noiseLevel: string;

    constructor(id: string, noiseLevel: string) {
        super(id);
        this.noiseLevel = noiseLevel;
    }
}

function doSomething(param: A | B | C): string {
    if (param instanceof Base) {
        return 'base';
    } else if (param instanceof A) {
        return 'a';
    } else if (param instanceof B) {
        return 'b';
    } else if (param instanceof C) {
        return 'c';
    }
}

Say I tried the following:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>doSomething(new A('idA', 'image');
doSomething(new B('idB', 1);
doSomething(new C('idC', 'high');
</code>
<code>doSomething(new A('idA', 'image'); doSomething(new B('idB', 1); doSomething(new C('idC', 'high'); </code>
doSomething(new A('idA', 'image');

doSomething(new B('idB', 1);

doSomething(new C('idC', 'high');

I would expect them to all return with the string 'base', because they are all subclasses of Base, and this was the first condition in the sequence to be checked.

I was OK with that, as I’m aware of polymorphism… but I wasn’t able to even write the function. And the reason why is what’s leaving me a bit confused.

When I hover over the word param for any of the else if‘s I get the error:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>The left-hand side of an 'instanceof' expression must be of type 'any', an object type, or a type parameter. ts(2358)
(parameter) param: never
</code>
<code>The left-hand side of an 'instanceof' expression must be of type 'any', an object type, or a type parameter. ts(2358) (parameter) param: never </code>
The left-hand side of an 'instanceof' expression must be of type 'any', an object type, or a type parameter. ts(2358)
(parameter) param: never

I can’t seem to find much about this online, at least nothing that really answers this for me.

Is it insinuating that Base is not a valid type? There isn’t anything particularly wrong with Base itself, it’s not something strange like an abstract class or anything like that (at least not at face value)… of course, it’s being used as a superclass in our scenario, but the wording of the error makes it sound like the problem is with Base as a singularity. If the problem was with Base by itself, surely this error would have been flagged for the initial if too? But it isn’t.

Hopefully, someone can help shine a bit of light on this. I’m finding it challenging as a beginner!

Thank you.

EDIT:

I wasn’t very clear at the start, my apologies.

My compiler won’t even let me write the function:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>function doSomething(param: A | B | C): string {
if (param instanceof Base) {
return 'base';
} else if (param instanceof A) {
return 'a';
} else if (param instanceof B) {
return 'b';
} else if (param instanceof C) {
return 'c';
}
}
</code>
<code>function doSomething(param: A | B | C): string { if (param instanceof Base) { return 'base'; } else if (param instanceof A) { return 'a'; } else if (param instanceof B) { return 'b'; } else if (param instanceof C) { return 'c'; } } </code>
function doSomething(param: A | B | C): string {
    if (param instanceof Base) {
        return 'base';
    } else if (param instanceof A) {
        return 'a';
    } else if (param instanceof B) {
        return 'b';
    } else if (param instanceof C) {
        return 'c';
    }
}

Ergo, I was never even able to get as far as:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>doSomething(new A('idA', 'image');
doSomething(new B('idB', 1);
doSomething(new C('idC', 'high');
</code>
<code>doSomething(new A('idA', 'image'); doSomething(new B('idB', 1); doSomething(new C('idC', 'high'); </code>
doSomething(new A('idA', 'image');

doSomething(new B('idB', 1);

doSomething(new C('idC', 'high');

5

Try param: Base | A | B | C, maybe the compiler is complaining since you are checking instanceof Base which it thinks applies only to the case where its checking for a pure Base instance and not A, B, or C. Basically, the typescript type system cannot a account for the niche behavior in javascript that instanceof the base class will return true.

Either way, I recommend being more explicit about type guards

like

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>
class Base {
name: "Base"
constructor(){
this.name="Base"
}
static is(value: unknown): value is Base{
return value instanceof Base
}
static isLike(value: unknown): value is Base | BaseData {
return typeof value === "object" && value !== null && (value as BaseData).name==="Base"
}
static from(obj: Base | BaseData){
if(Base.is(obj)){
return obj
}
else {
// specific to your class and its constructor
return new Base(...specific code...)
}
}
toObject(): BaseData {
return Object.assign({},this)
}
}
type BaseData = {
[K in keyof Base]: Base[K]
}
</code>
<code> class Base { name: "Base" constructor(){ this.name="Base" } static is(value: unknown): value is Base{ return value instanceof Base } static isLike(value: unknown): value is Base | BaseData { return typeof value === "object" && value !== null && (value as BaseData).name==="Base" } static from(obj: Base | BaseData){ if(Base.is(obj)){ return obj } else { // specific to your class and its constructor return new Base(...specific code...) } } toObject(): BaseData { return Object.assign({},this) } } type BaseData = { [K in keyof Base]: Base[K] } </code>

class Base {
  name: "Base"
  constructor(){
    this.name="Base"
  }
  static is(value: unknown): value is Base{
    return value instanceof Base
  }
  static isLike(value: unknown): value is Base | BaseData {
    return typeof value === "object" && value !== null && (value as BaseData).name==="Base"
  }
  static from(obj: Base | BaseData){
    if(Base.is(obj)){
      return obj
    }
    else {
      // specific to your class and its constructor
      return new Base(...specific code...)
    }
  }
  toObject(): BaseData {
    return Object.assign({},this)
  }
}

type BaseData = {
  [K in keyof Base]: Base[K]
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật