Skip to content

Commit

Permalink
Merge pull request #51085 from callstack-internal/docs/add-typescript…
Browse files Browse the repository at this point in the history
…-docs-about-complex-types

[No QA] Docs: Added entry about complex types in functions parameters
  • Loading branch information
mountiny authored Oct 22, 2024
2 parents addb43b + 676ef8c commit d329b9c
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions contributingGuides/STYLE.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- [Refs](#refs)
- [Other Expensify Resources on TypeScript](#other-expensify-resources-on-typescript)
- [Default value for inexistent IDs](#default-value-for-inexistent-IDs)
- [Extract complex types](#extract-complex-types)
- [Naming Conventions](#naming-conventions)
- [Type names](#type-names)
- [Prop callbacks](#prop-callbacks)
Expand Down Expand Up @@ -492,6 +493,30 @@ const foo = report?.reportID ?? '-1';
report ? report.reportID : '-1';
```

### Extract complex types

Advanced data types, such as objects within function parameters, should be separated into their own type definitions. Callbacks in function parameters should be extracted if there's a possibility they could be reused somewhere else.

```ts
// BAD
function foo(param1: string, param2: {id: string}) {...};

// BAD
function foo(param1: string, param2: (value: string) => void) {...};

// GOOD
type Data = {
id: string;
};

function foo(param1: string, param2: Data) {...};

// GOOD
type Callback = (value: string) => void

function foo(param1: string, param2: Callback) {...};
```

## Naming Conventions

### Type names
Expand Down

0 comments on commit d329b9c

Please sign in to comment.