-
-
Notifications
You must be signed in to change notification settings - Fork 25
Complete TypeScript guidelines #69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Off the top of my head, here's a list of guidelines we could include:
There are also a couple of tickets we've created already: |
To get to an MVP we are going to do the enum bullet, don't use any bullet and the type assertions bullet. We will come back to additional items later. Pointing based on this comment. |
Additional ideas (WIP): Prefer
|
This comment was marked as resolved.
This comment was marked as resolved.
WIP: This may or may not be appropriate to include in the guidelines. The focus of the guidelines should be on conveying preferred practices to already knowledgeable readers, not providing instructions about TypeScript. However, having a glossary to establish terminology used throughout the guidelines might be useful. This terminology could then be used in code review as well. Type System ConceptsAssignee vs. Assigned
// Assignment operation - LHS: 'Assignee', RHS: 'Assigned'
const assignee: Assignee = assigned;
// Function call - parameter: 'Assignee', argument: 'Assigned'
function example(param: Assignee) {}
example(assigned);
// Generic parameters with constraints
type ExampleGeneric<Param extends Assignee> = Param;
type ExampleType = GenericExample<Assigned>;
// Relationship - The 'Assigned' type is a subtype of the 'Assignee'
type IsTrue = Assigned extends Assignee ? true : false; Widest Subtype vs. Narrowest Supertype
Signature vs. Constraint
Subtyping shorthands
Function typesUniversal function supertype🚫 type AnyParamsFunctionType = (...args: any[]) => any;
type NeverParamsFunctionType = (...args: never[]) => unknown; ✅ type FunctionSupertype = ((...args: never) => unknown) | ((...args: never[]) => unknown) A. The return type of this supertype function needs to be This is because function parameters are contravariant, meaning
B. In general, array types of arbitrary length are not assignable to tuple types that have fixed types designated to some or all of its index positions (the array is wider than the tuple since it has less constraints). This means that there's a whole subcategory of functions that
Universal function subtypeSometimes function types that look like the following become necessary: type FunctionSubtype = (...args: unknown[]) => never;
If the function type being defined needs to be assignable to other functions, then the universal function supertype is not the right choice. |
@desi convert this to an epic. Once converted to an epic remove the estimate and create additional issues that represent the comments and statements above. |
@MajorLift Your glossary seems like it could be quite helpful, even for people who think they are familiar with TypeScript. I too am not sure where it belongs exactly - perhaps another document, or maybe at the very bottom of the current document? We can always add it later, though. |
It might make sense to have a dedicated doc for type concepts with explanations about some of the more complex types we use. I'll keep collecting material here until it reaches a critical mass. |
…cape Hatches (#74) ## Motivation In order to improve type safety and maintainability, we need to establish clear guidelines regarding how to apply types: specifically, when and when not to use explicit type declarations or keywords such as `as`, `any`. ## Explanation - See markdown preview: [TypeScript Guidelines - "Types" section](https://github.com/MetaMask/contributor-docs/blob/240206-typescript-guidelines-any-as/docs/typescript.md) - ~The merge conflicts with main will be resolved after the review process to avoid noisy diffs.~ - All examples are tagged with a permalink for easy reference and quotation. ### Table of Contents - Types - Type Inference - Type Annotations (`:`, `satisfies`) - Type Assertions (`as`, `!`) - Escape Hatches (`any`, `@ts-expect-error`) ### Details - Dangers and advantages. - Tips for avoiding. - Acceptable use cases. ## References - Closes #47 - Closes #57 - Contributes to #69 --------- Co-authored-by: Mark Stacey <[email protected]> Co-authored-by: Elliot Winkler <[email protected]>
Closed by #74, which, together with #80, covers the initial topics listed in #69 (comment). The content suggested by later comments will be preserved here and included in future contributor-docs articles. |
Uh oh!
There was an error while loading. Please reload this page.
We've started a document which is intended to contain TypeScript guidelines, but it is severely lacking in content.
We should get to an MVP by adding the following guidelines:
The text was updated successfully, but these errors were encountered: