简体   繁体   中英

typescript parameter to an object declaration

I'm new to typescript, want to convert the following values to an object so that I may reuse them in the form of an object variable.

 abc.create ( param1, {
            main: this.ODDD,
            filter: this.RLL
        }, param3)

I'm trying to declare a variable 'param2' so that I may use the second parameter which is of type Object so that I may use it at other places. I'm trying to do it like this but can't work out.

 var param2: Object = {
        main: string, 
        filter: string
    };

but it doesn't allow me to create this or assign values, the error is: "string only refers to a type but it is used as a value here".

Have tried this and this link but didn't work.

Any ideas?

You need to create interfaces to achieve the same

interface Param {
     main: string,
     filter: string, 
}

let abc = {
   create (param1, param2: Param, param3) {
        //your code
   } 
}

let param2: Param = <Param>{
  main: "string content",
  filter: "string content2"
}

abc.create(param1, param2, param3);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM