typescript - Type problems while overloading constructors -


i having problems while overloading constructors, won't let me tell type variable contains. how can force type, or make work anyway...?

constructor(points: point[], name?: string); constructor(walls: wall[], name?: string); constructor(pointsorwalls: (wall | point)[], name?: string) {     if (pointsorwalls[0] instanceof point) {         //  if first point must points          //  here typescript says pointsorwalls of type (wall | point)[]         this.walls = pointsorwalls.map(function(point, ind, points) {             return new wall(point, points[++ind % points.length])         })     }else{         //  since these aren't points walls         this.walls = walls     }     this.name = name } 

how can force type, or make work anyway...

use type assertion:

//  if first point must points let points = pointsorwalls point[]; 

complete:

class wall {w} class point {p}  class foo {     walls;     name     constructor(points: point[], name?: string);     constructor(walls: wall[], name?: string);     constructor(pointsorwalls: (wall | point)[], name?: string) {         if (pointsorwalls[0] instanceof point) {             //  if first point must points             let points = pointsorwalls point[];              //  here typescript says pointsorwalls of type (wall | point)[]             this.walls = points.map(function(point, ind, points) {                 return new wall(point, points[++ind % points.length])             })         }else{             //  since these aren't points walls             this.walls = walls         }         this.name = name     }    } 

more

https://basarat.gitbooks.io/typescript/content/docs/types/type-assertion.html


Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -