Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

260
Views
Runtime type introspection with TypeScript

I have two types of filter functions: NodeFilter and EdgeFilter e.g. type NodeFilter = (node: Node) => boolean

I'd like to write a function Graph::filter(filters: NodeFilter | EdgeFilter): Graph which applies the filters to nodes and edges of the Graph, to create a new Graph.

So in filter I need to do

if (filter is type NodeFilter) nodes = nodes.filter(filter)
else if (filter is type EdgeFilter) edges = edges.filter(filter)

I've seen recommendations of adding a "type" property to instances. e.g.

function myNodeFilter(node: Node): boolean { // do the filtering and return boolean }
myNodeFilter.type = "NodeFilter";

Is there a better way? Maybe defining a callable class instance?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use descriminated unions for this with a sprinkle of call signature.


/**
* Kindly ignore the naming,
* it was conflicting with the DOM Node and NodeFilter function (since I tested this in typescript playground)
**/
type NodeF = {};
type Edge = {}
type NodeFilterr = {
    (node: NodeF): boolean,
    type: "NodeFilter"
}

type EdgeFilter = {
    (edge: Edge): boolean,
    type: "EdgeFilter"
}

function f(filter: NodeFilterr | EdgeFilter ) {
    if ( filter.type === "NodeFilter" ) {
        // do stuff
    }
    if ( filter.type === "EdgeFilter" ) {
        // do stuff
    }
}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!