0

I don't really know the terms to search on google for this, so it might be duplicated.

I have an interface that looks like

export interface SolidOptions<T> { position: Cartesian3; options?: T; } 

now, I want this T to be ONLY of 3 types : CylinderOptions | RectangleOptions | PolygoneOptions

I do NOT want this solution :

export interface SolidOptions<T> { position: Cesium.Cartesian3; options?: CylinderOptions | RectangleOptions | PolygoneOptions; } 

the reason why is I want to use this as follow :

 static generateCylinder = (options: SolidOptions<CylinderOptions>)=> { } static generateRectangle = (options: SolidOptions<RectangleOptions >)=> { } static generatePolygon = (options: SolidOptions<PolygoneOptions>)=> { } 

and not having the abilities to pass the wrong type to the wrong function.

1 Answer 1

3

You can define constraint on generic type parameter:

export interface SolidOptions<T extends CylinderOptions | RectangleOptions | PolygoneOptions> { position: Cartesian3; options?: T; } 
Sign up to request clarification or add additional context in comments.

2 Comments

Actually I knew it, but it was triggering me that I could pass anything to it. But yes in the end, if I ignore the other properties it doesn't matter in the end.
You would only be able to pass the provided values in the extends - any values which matches those types. If those types are too general then it may allow more than you'd like.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.