1

How can I define multiple constructors in typescript? For example, I want to have the following code:

class Folder extends Asset { constructor(repositoryId: string, assetId: string) { super(); } constructor (folder: Folder) { } } 

Is it possible to instantiate a class multiple ways in typescript?

2
  • 2
    Does this answer your question? Constructor overload in TypeScript Commented Dec 22, 2021 at 14:39
  • 1
    You can have multiple overloads "on type" level, but only single implementation allowed. (It will need to be able to handle string | Folder as a first parameter) typescriptlang.org/play?#code/… Commented Dec 22, 2021 at 14:45

1 Answer 1

3

You trt somenthing like this:

class Folder extends Asset { constructor(repositoryId: string, assetId: string); constructor(folder: Folder); constructor(repositoryId?: string, assetId?: string, folder?: Folder) if (!folder && repositoryId && assetId) super(); else { // To stuff } } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.