Raphael Rafatpanah
Raphael Rafatpanah

Reputation: 20007

How to annotate the type of a function within an object?

What is the proper way to type this object?

{a: 1, b: n => n + 1}

Here's my attempt.

let foo: {a: number, b: number => number} = {a: 1, b: n => n + 1}

However, this isn't the correct syntax.

Upvotes: 0

Views: 88

Answers (2)

Jeff Bowman
Jeff Bowman

Reputation: 95764

Function type expressions need parentheses and parameter names. From the handbook:

Note that the parameter name is required. The function type (string) => void means “a function with a parameter named string of type any“!

let foo: {a: number, b: (n: number) => number} = {a: 1, b: n => n + 1}

Playground Link

Upvotes: 3

peinearydevelopment
peinearydevelopment

Reputation: 11474

Function syntax documentation

let foo: {a: number, b: (n: number) => number} = {a: 1, b: n => n + 1}

Playground Link

Upvotes: 3

Related Questions