basarat / basarat/typescript-book
Callable Objects
- 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
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