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

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

routes - Laravel 4 Wildcard Routing to Different Controllers -

cross browser - XSLT namespace-alias Not Working in Firefox or Chrome -