instanceof function parameter returns false in Typescript/Angular

Nicholas

I'm trying to find out how to get the type of an Object if it was passed as a parameter in my function.

For example you have following code

getTypeOfAnimal(animal){
   console.log(animal instanceof Cat)
   console.log(animal instanceof Bear)
}

animal could be either an instanceof a Cat or a Bear. The animal passed in from my form always returns false (also if the Object's structure seems to match one of a Cat or a Bear).

Is there a way to get the type properly?

smnbbrv

The root of the problem is this line

also if the Object's structure seems to match one of a Cat or a Bear

The object structure won't matter here. The instanceof checks whether the object was created using a constructor of Cat or Bear, e.g. new Cat() (and actually if both extend Animal then the instanceof Animal would be true for both).

Normally TypeScript uses duck typing to resolve whether the interface is matching the object structure and this is where the similar object structure could work. However instanceof is JavaScript (because is executed in the runtime), so there is no much more TypeScript can do there. That is why it does not work as you might have expected.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

In which case PHP sort function returns FALSE?

PHP instanceof returns false for true condition

Recursive function return incorrectly returns false

Why instanceof sometimes doesn't compile and sometimes returns false?

Woocommerce get_item() function returns false

Why instanceof returns false for a child object in Javascript

in_array function always returns false

instanceof returns false for children class

Includes function returns apparently wrong false

JS, function returns undefined instead of true or false

Ensure my function returns mutated object as instanceof the same class typescript?

react passing the parameter to a function that returns an async function

Function that only returns false returns nothing

A function that returns an array, but that also takes a function as parameter

isalpha() function returns false for character in string

Php-Image function returns false?

JS Checking return function with if, always returns false

vb function returns true when false

Function returns undefined instead of true/false

Why wp_mail() function returns 'false'?

My function always returns false the first time

Function returns incorrect False (only at higher values)

Why does Predicates.instanceOf returns false?

Prime number function returns true instead of false

Function that accepts parameter and returns +1

Recursive Function Always Returns False

React tetris. Function always returns false

Comparing Two Equal Values in a Function Returns False When Parameter is Integer Represented as a String

PHP function returns false even if executed successfully

TOP Ranking

  1. 1

    What exactly is memory mapped io and port based io

  2. 2

    How i extract text from a model dialog in selenium?

  3. 3

    pump.io port in URL

  4. 4

    Selenium browsing with headless isn't working?

  5. 5

    change TabView indicator SwiftUI

  6. 6

    python requests How to send data to a website

  7. 7

    Kubernetes pods not starting, running behind a proxy

  8. 8

    Scaffold-DbContext returns System.ArgumentNullException: Parameter name: proposedIdentifier in EFCore 2.0 when trying to pluralize

  9. 9

    Java POI: How to find an Excel cell with a string value and get its position (row) to use that position to find another cell

  10. 10

    Creating interaction sessions based on timestamps in pandas

  11. 11

    Maven annotation processing processor not found

  12. 12

    Missing field when deserializing using Jackson (Polymorphic)

  13. 13

    Spring Data JPA repository methods don't recognize property names with underscores

  14. 14

    Get all Woocommerce products from current product category term Id in a WP_Query

  15. 15

    How to update if key exists - sequelize

  16. 16

    I got message for error_log file "The stream or file ".../laravel.log" could not be opened: failed to open stream: Permission denied"

  17. 17

    Servlet: HTTP Status 404 - Not Found

  18. 18

    Type 'Moment' is not assignable to type 'Date'. Property 'toDateString' is missing in type 'Moment'

  19. 19

    what is key in CTTelephonyNetworkInfo().serviceSubscriberCellularProviders and CTTelephonyNetworkInfo().serviceCurrentRadioAccessTechnology

  20. 20

    Laravel(ajax) 404 not found

  21. 21

    Error: Cannot write a value to a ko.computed unless you specify a 'write' option. If you wish to read the current value, don't pass any parameters

HotTag

Archive