basarat / basarat/typescript-book

Callable Objects

Open
#154 0 comments 4 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
21.6k
Forks
2.6k
PR merge metrics
No merged PRs in 30d

Description

Historically I've done:

``` ts
// Declaration
interface Foo {
(): string; // Callable
displayName: string; // Some object property
}
```

Which is great for a `.d.ts` file:

``` ts
declare var foo: Foo
```

But awful looking for when you have to do your own creation:

``` ts
// Creation using assertion (eww)
const foo: Foo = function() {
return "hello";
} as any;
foo.displayName = "foo";
```

@pksorensen just pointed out https://twitter.com/pksorensen/status/767128252326506500

``` ts
function foo() {
return "hello";
}
namespace foo {
export const displayName = "foo"
}
```

> Add to Tips

Contributor guide

Open the contributing guide

Research direction

Locate the book's Tips section and review nearby entries for placement and formatting. Add a tip explaining callable objects, including the provided declaration, assertion-based creation, and function/namespace pattern; done means the example is included clearly in the book.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.