0

I'm converting response to a Class like this:

private mapSingle(response: SingleResponse): CodeExample {
    var authorResponse = response.modular_content[response.item.elements.author.value as string];
    var author = new Author(authorResponse.system, authorResponse.elements.name.value, authorResponse.elements.image.value);
    return new CodeExample(
        response.item.system,
        response.item.elements.code.value,
        author,
        response.item.elements.versions.value as CodeExampleCategory[],
        response.item.elements.title.value,
        response.item.elements.versions.value as CodeExampleVersion[]
    );
}

Even though this works perfectly fine, I'm hoping there is a better way to do this so that I don't need to declare the author response before the constructor. Ideally I would like to put my own function in constructor and resolve this "inline". Is this possible?

1 Answer 1

1

From your coding style, my guess is that you have not had previous experiences with javascript before picking up typescript. If I'm wrong I apologize. The reason I say this is because developers from other languages have the ability to overload their constructors/methods by using different method signatures but in typescript/javascript, that is not the case.

Ideally I would like to put my own function in constructor and resolve this "inline". Is this possible?

I apologize if this doesn't directly answer your question but my intuition tells me that you probably want to solve your by overloading your constructor. Since typescript/javascript, doesn't let you do this, there is another 'pattern'/'best practice' to make the constructor take in a configuration object instead of many parameters.

So to tie everything together, instead of having your constructors take in many arguments, your constructor should take in one configuration object. You can declare an interface with optional parameters for this options and then satisfy the interfaces with an object literal.

interface CodeExampleOptions {
    requiredParam: string,
    someOptionalParam?: string,
    authorResponse?: Author
}

class CodeExample {
    constructor(options: CodeExampleOptions) {
        // do something with options
        if (options.authorResponse !== undefined) {
            // do something with the authorResponse 
        }
    }
}

// now your method could look something like this.
private mapSingle(response: SingleResponse): CodeExample {
    return new CodeExample({
        // someOptions: 'some values',
        authorResponse: response.modular_content[response.item.elements.author.value as string],
        // someOtherOptions: 'some other values'
    });
}

This is how I address wanting to 'overload constructors' in typescript if that's what you were hoping to do.

Hope that helps!

Sign up to request clarification or add additional context in comments.

1 Comment

Hey there, thanks for the awesome response! My main goal was to be able to make the code cleaner/shorter by having functions inside object initialization so that I don't have to declare those outside or wrap them in a separate function. I'll try this approach, see how that works and will get back to this answer, Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.