Typescript: How to use interface and default parameters together?

Created on 16 Sep 2015  路  2Comments  路  Source: microsoft/TypeScript

Code below

enum Type { digit=1, alpha=2, alnum=3 }
enum Transform{uppercase=1,lowercase}
interface Options {
    type: Type;
    length: number;
    min: number;
    max: number;
    uppercase: boolean;
    lowercase: boolean;
    transform: Transform;
    valueChange: (element:Object,value:string) => string;
}
class InputFilter {
    constructor(private options: Options, private element: Object) {

    }

}

I wanna to makeoptionshave defaulte value,like this:

options = {
        "type": "alnum",
        "length": null,
        "min": 0,
        "max": Infinity,
        "uppercase": true,
        "lowercase": true,
        "transform": null,
        "valueChange": function(element, value) {}
    };

How can I do?Wating online :).

Question

Most helpful comment

Hi @codelegant, you could have both a type annotation and an initializer for your parameter property:

class InputFilter {
    constructor(private options: Options = {
                    type: "alnum",
                    length: null,
                    min: 0,
                    max: Infinity,
                    uppercase: true,
                    lowercase: true,
                    transform: null,
                    valueChange(element, value) {}
                },
                private element: {}) {

    }

}

Or you could make Options a class:

class Options {
    type = "alnum";
    length = null;
    min = 0;
    max = Infinity;
    uppercase = true;
    lowercase = true;
    transform = null;
    valueChange(element, value) { }
}

class InputFilter {
    constructor(private options = new Options(),
                private element: {}) {

    }
}

However, in the future, any questions that are not about whether there is a bug in the compiler should probably be posted on StackOverflow.

All 2 comments

Hi @codelegant, you could have both a type annotation and an initializer for your parameter property:

class InputFilter {
    constructor(private options: Options = {
                    type: "alnum",
                    length: null,
                    min: 0,
                    max: Infinity,
                    uppercase: true,
                    lowercase: true,
                    transform: null,
                    valueChange(element, value) {}
                },
                private element: {}) {

    }

}

Or you could make Options a class:

class Options {
    type = "alnum";
    length = null;
    min = 0;
    max = Infinity;
    uppercase = true;
    lowercase = true;
    transform = null;
    valueChange(element, value) { }
}

class InputFilter {
    constructor(private options = new Options(),
                private element: {}) {

    }
}

However, in the future, any questions that are not about whether there is a bug in the compiler should probably be posted on StackOverflow.

my solution

export class Doctors{
    constructor(
        name= "Ali 艦en",
        photo= "http://res.cloudinary.com/doctor.svg",
        cv= "Ozgecmis kisaca"
    ){ }
}

export class Corporate{
    constructor(
        doctors:Doctors[] =  [new Doctors()]
    )
    { }
Was this page helpful?
0 / 5 - 0 ratings

Related issues

DanielRosenwasser picture DanielRosenwasser  路  3Comments

blendsdk picture blendsdk  路  3Comments

siddjain picture siddjain  路  3Comments

kyasbal-1994 picture kyasbal-1994  路  3Comments

Zlatkovsky picture Zlatkovsky  路  3Comments