0

I have a class in typescript with several params, which are empty in the constructor. I would like to have a default value for one of the optional params:

export class SomeClass {

    pageSize: number = 10;

    constructor(pagesize?: number){
        this.pageSize = pageSize;
    }
}

If I instantiate the class like this:

let k = new SomeTest()

The pageSize is undefined

How could I initialize a optional property?

Thank you!

1 Answer 1

1

Provide the default value in the constructor:

export class SomeClass {

    pageSize: number;

    constructor(pagesize: number = 10){
        this.pageSize = pageSize;
    }
}

You can also define the property in the constructor:

export class SomeClass {
    constructor(public pagesize = 10){
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.