To create a function with typed parameters in TypeScript, you can define an interface for the parameter type or use a built-in type such as number
, string
, or boolean
.
Here's an example of a TypeScript function with an interface for its parameter:
index.ts162 chars9 lines
In the example above, we define an interface Person
with two properties: name
of type string
and age
of type number
. Then, we declare a function greet
that takes a parameter of type Person
.
You can call the greet
function with an object that conforms to the Person
interface like this:
index.ts107 chars4 lines
Note that TypeScript will throw an error if you try to call greet
with an object that does not have the required properties of the Person
interface.
gistlibby LogSnag