Typescript - pass interface literal to function parameter


Paul Redmond

I want to use an interface to set data types and then call them in a function while setting default values ​​without passing the data .

I am getting the after canvas error ',' expected.in the function . Can't I call it that?

// Options
interface options {
    canvas?: {
        width: number;
        height: number;
        bgColor: string;
    };
    brushes?: {
        sizeSm: number;
        sizeLg: number;
    };
}

function initialize(options {canvas.width = 700, brushes.sizeSm = 20}) {
    // Do stuff
}

// Call function
initialize({
    canvas: {
        width: 820,
        height: 450,
        bgColor: '#fff'
    },
    brushes: {
        sizeSm: 10,
        sizeLg: 20
    },
});
Radim Kohler

this should work

interface options {
    canvas?: {
        width: number;
        height?: number;
        bgColor?: string;
    };
    brushes?: {
        sizeSm: number;
        sizeLg?: number;
    };
}

function initialize(opts: options = {canvas : { width : 700}, brushes: {sizeSm : 20}} ) {
//function initialize(options {canvas.width = 700, brushes.sizeSm = 20}) {
    // Do stuff
}

// Call function
initialize({
    canvas: {
        width: 820,
        height: 450,
        bgColor: '#fff'
    },
    brushes: {
        sizeSm: 10,
        sizeLg: 20
    },
});

First, the syntax of the options as arguments should be adjusted to use the interface .opts: options

Next, if we want one default value, we need to pass it properly:

function initialize(opts: options = {canvas : { width : 700}, brushes: {sizeSm : 20}} ) {

and since we are only setting width and sizeSm.. the rest are adjusted to be optional (eg heigth?:number)

play here

Related


Typescript - pass interface literal to function parameter

Paul Redmond I want to use an interface to set data types and then call them in a function while setting default values without passing the data . I am getting the after canvas error ',' expected.in the function . Can't I call it that? // Options interface opt

Typescript - pass interface literal to function parameter

Paul Redmond I want to use an interface to set data types and then call them in a function while setting default values without passing the data . I am getting the after canvas error ',' expected.in the function . Can't I call it that? // Options interface opt

Typescript - pass interface literal to function parameter

Paul Redmond I want to use an interface to set data types and then call them in a function while setting default values without passing the data . I am getting the after canvas error ',' expected.in the function . Can't I call it that? // Options interface opt

Typescript - pass interface literal to function parameter

Paul Redmond I want to use an interface to set data types and then call them in a function while setting default values without passing the data . I am getting the after canvas error ',' expected.in the function . Can't I call it that? // Options interface opt

Typescript pass function as parameter

Joachim connectWebSocket() { const socket = new SockJS('http://localhost:8080/websocket'); this.stompClient = Stomp.over(socket); const _this = this; this.stompClient.connect({ "Authorization" : "Bearer "+localStorage.getItem("Authorization"

Pass an interface as a function parameter (PHP)?

username I'm watching one of Jeffs Laracast's tutorials on coding rules. function signUp($subscription) { if ($subscription == 'monthly') { $this->createMonthlySubscription(); } elseif ($subscription == 'forever') { $this->

Pass an interface as a function parameter (PHP)?

username I'm watching one of Jeffs Laracast's tutorials on coding rules. function signUp($subscription) { if ($subscription == 'monthly') { $this->createMonthlySubscription(); } elseif ($subscription == 'forever') { $this->

Pass an interface as a function parameter (PHP)?

username I'm watching one of Jeffs Laracast's tutorials on coding rules. function signUp($subscription) { if ($subscription == 'monthly') { $this->createMonthlySubscription(); } elseif ($subscription == 'forever') { $this->

Pass an interface as a function parameter (PHP)?

username I'm watching one of Jeffs Laracast's tutorials on coding rules. function signUp($subscription) { if ($subscription == 'monthly') { $this->createMonthlySubscription(); } elseif ($subscription == 'forever') { $this->

Pass an interface as a function parameter (PHP)?

username I'm watching one of Jeffs Laracast's tutorials on coding rules. function signUp($subscription) { if ($subscription == 'monthly') { $this->createMonthlySubscription(); } elseif ($subscription == 'forever') { $this->

Convert interface to Function parameter in Typescript

Aylton Almeida What I want to know is if it is possible to use an interface to describe the parameters that need to be passed to a function. An example is as follows: interface Person { name: string; age: number; } function createPerson(n

Convert interface to Function parameter in Typescript

Aylton Almeida What I want to know is if it is possible to use an interface to describe the parameters that need to be passed to a function. An example is as follows: interface Person { name: string; age: number; } function createPerson(n

Convert interface to Function parameter in Typescript

Aylton Almeida What I want to know is if it is possible to use an interface to describe the parameters that need to be passed to a function. An example is as follows: interface Person { name: string; age: number; } function createPerson(n

Convert interface to Function parameter in Typescript

Aylton Almeida What I want to know is if it is possible to use an interface to describe the parameters that need to be passed to a function. An example is as follows: interface Person { name: string; age: number; } function createPerson(n

Typescript: Pass function as type in interface

Samuel Hudec I'm trying to figure out how to get a type from an existing Typescript function and use it to define an interface. I'm working on a React project and I want to pass ( action creatorfunctionality) into Propsan interface and then as a pass into a Re

Typescript: Pass function as type in interface

Samuel Hudec I'm trying to figure out how to get a type from an existing Typescript function and use it to define an interface. I'm working on a React project and I want to pass ( action creatorfunctionality) into Propsan interface and then as a pass into a Re

Typescript: Pass function as type in interface

Samuel Hudec I'm trying to figure out how to get a type from an existing Typescript function and use it to define an interface. I'm working on a React project and I want to pass ( action creatorfunctionality) into Propsan interface and then as a pass into a Re

Typescript: Pass function as type in interface

Samuel Hudec I'm trying to figure out how to get a type from an existing Typescript function and use it to define an interface. I'm working on a React project and I want to pass ( action creatorfunctionality) into Propsan interface and then as a pass into a Re

Typescript: Pass function as type in interface

Samuel Hudec I'm trying to figure out how to get a type from an existing Typescript function and use it to define an interface. I'm working on a React project and I want to pass ( action creatorfunctionality) into Propsan interface and then as a pass into a Re

Typescript: Pass function as type in interface

Samuel Hudec I'm trying to figure out how to get a type from an existing Typescript function and use it to define an interface. I'm working on a React project and I want to pass ( action creatorfunctionality) into Propsan interface and then as a pass into a Re

typescript - pass type to function parameter

LTFoReal I'm pretty new to Typescript, but I'm trying to pass a custom type to a function. In this case I avoid "any", here is my example. interface ReturnObj<T> { returnObj: T | null } const createResultObj = ( isSuccessful: boolean = false, return

typescript - pass type to function parameter

LTFoReal I'm pretty new to Typescript, but I'm trying to pass a custom type to a function. In this case I avoid "any", here is my example. interface ReturnObj<T> { returnObj: T | null } const createResultObj = ( isSuccessful: boolean = false, return