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(name: string, age: number) {}

I want to use the Person interface as a reference for the createPerson parameter . So far I've been using this:

function createPerson(name: Person['name'], age: Person['age']) {}

Although it's better than nothing, I still don't know if this is the best way for me since I still need to rewrite all parameter names. Are there better options? Thanks in advance.

TJ crowd

I'm 99.9% sure there's no way to "extend" an interface's type definition like this, especially since interfaces don't have an order when function arguments execute.

You can do what is shown in the question directly, or accept an Personobject:

function createPerson(person: Person) { /* ... */ }

or destructively:

function createPerson({name, age}: Person) { /* ... */ }

Either way, you can call it with an object like:

const createdPerson = createPerson({name: "Aylton Almeida", age: 20});

Related


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 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 function with interface type attribute parameter

Delta Tango Is there a way to use an interface's properties/keys as function parameter types? For example, if I have an interface: interface column{ id: string, title: string, description: string } I have a function: const replaceColumnProperty = (co

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

Convert []interface{} to parameter of non-variadic function

Tired_of_nitpickers: I'm looking for an elegant way to unpack parameter lists in Go. I don't want to use a variadic function for this, because in the use case where I'm writing the use case, I already know the number of parameters and want to keep this part si

Convert []interface{} to parameter of non-variadic function

Tired_of_nitpickers: I'm looking for an elegant way to unpack parameter lists in Go. I don't want to use a variadic function for this, because in the use case where I'm writing the use case, I already know the number of parameters and want to keep this part si

Convert []interface{} to parameter of non-variadic function

Tired_of_nitpickers: I'm looking for an elegant way to unpack parameter lists in Go. I don't want to use a variadic function for this, because in the use case where I'm writing the use case, I already know the number of parameters and want to keep this part si

Convert []interface{} to parameter of non-variadic function

Tired_of_nitpickers: I'm looking for an elegant way to unpack parameter lists in Go. I don't want to use a variadic function for this, because in the use case where I'm writing the use case, I already know the number of parameters and want to keep this part si

Map signature from TypeScript interface to function's parameter

axon For someone reading the TypeScript manual, this might be a very simple question: Consider the following interface: (from a library simple-gitI want to use ). export interface SimpleGitTaskCallback<T = string, E extends GitError = GitError> { (err: nul

Map signature from TypeScript interface to function's parameter

axon For someone reading the TypeScript manual, this might be a very simple question: Consider the following interface: (from a library simple-gitI want to use ). export interface SimpleGitTaskCallback<T = string, E extends GitError = GitError> { (err: nul

Map signature from TypeScript interface to function's parameter

axon For someone reading the TypeScript manual, this might be a very simple question: Consider the following interface: (from a library simple-gitI want to use ). export interface SimpleGitTaskCallback<T = string, E extends GitError = GitError> { (err: nul