This repository has been archived on 2022-08-19. You can view files and clone it, but cannot push or open issues or pull requests.
hydrogen-web/doc/TS-MIGRATION.md

14 lines
1.2 KiB
Markdown
Raw Normal View History

2021-11-30 19:47:51 +05:30
# Typescript style guide
2021-11-30 19:45:25 +05:30
## Use `type` rather than `interface` for named parameters and POJO return values.
2021-12-01 16:05:16 +05:30
`type` and `interface` can be used somewhat interchangeably, but let's use `type` to describe data and `interface` to describe (polymorphic) behaviour.
2021-11-30 19:45:25 +05:30
Good examples of data are option objects to have named parameters, and POJO (plain old javascript objects) without any methods, just fields.
Also see [this playground](https://www.typescriptlang.org/play?#code/C4TwDgpgBACghgJwgO2AeTMAlge2QZygF4oBvAKCiqmTgFsIAuKfYBLZAcwG5LqATCABs4IAPzNkAVzoAjCAl4BfcuVCQoAYQAWWIfwzY8hEvCSpDuAlABkZPlQDGOITgTNW7LstWOR+QjMUYHtqKGcCNilHYDcAChxMK3xmIIsk4wBKewcoFRVyPzgArV19KAgAD2AUfkDEYNDqCM9o2IQEjIJmHT0DLvxsijCw-ClIDsSjAkzeEebjEIYAuE5oEgADABJSKeSAOloGJSgsQh29433nVwQlDbnqfKA)
2021-12-01 16:05:16 +05:30
## Use `Record<string, any>` to describe a type that accepts any Javascript object.
2021-12-01 22:52:09 +05:30
`Record<string, any>` allows us to avoid passing in primitive types and prevents type errors when accessing properties. As this is a temporary type while converting javascript, it seems best to not add any additional requirements. If any errors occur, they must have already been present, and we should fix it by adding proper types. So prefer `Record<string, any>` over `[key: string]: any`, `any` or `object`.