Giter VIP home page Giter VIP logo

ts-guideline's Introduction

ts-guideline

Minimalistic configuration for TS to only extend JS with types. No TS-scpecific features, no bundling. Readable maintainable code after compilation.

  • commonjs folder for CommonJS Modules ts-configuration compatible with ECMAScript import

  • ecmascript folder for ECMAScript Modules ts-configuration

  • js-dts folder for JS + DTS configuration.

  • js-doc folder for JS Doc + TypeScript configuration

Important: use different file names sum.js - isum.d.ts to not create import conflicts

Reccomendations

  1. Avoid using type aliases - example from mongoose types

    export type ApplyBasicQueryCasting<T> = T | T[] | (T extends (infer U)[] ? U : any) | any;

    this is unreadable and overcomplicated

    Aliases are good only for simple types

    type Fruit = 'banana' | 'orange' | 'pineapple' | 'watermelon';
    
    type Debt = { amount: number; dueTo: Date };
    type AccountDebt = { accountId: number; debt: Debt | null };
  2. Never use complicated generic types - example from mongoose types

    type QueryWithHelpers<ResultType, DocType, THelpers = {}, RawDocType = DocType> = Query<
      ResultType,
      DocType,
      THelpers,
      RawDocType
    > &
      THelpers;

    basically it looks worse then

    type QueryWithHelpers<any> = Query<any> & any;
  3. Not everything is neccessary to cover with types.

    It requires to add useless interfaces for type like this. Better just not use this.

    // with typing
    interface IDynamicGeneratedClass {}
    
    const classGenerator = (): IDynamicGeneratedClass =>
      class DynamicGeneratedClass implements IDynamicGeneratedClass {
        args: number[];
        constructor(...args: number[]) {
          this.args = args;
        }
      };
    // simplified
    const classGenerator = () =>
      class DynamicGeneratedClass {
        args: number[];
        constructor(...args: number[]) {
          this.args = args;
        }
      };
  4. avoid using any - there is no point at all to use typescript if you need to keep using any.

This list will continue in future.

Alternatives

  1. You can use JS Doc @type for type definitions, typescript will work and check types for you

  2. You can always use JS + DTS - it is a similar way as it is done in C++ with .h and .cpp files DTS files are not working perfectly. Sometimes you forced to use JS Doc @typedef to import types from d.ts files

Summary

I prefer to use JS + DTS or JS DOC + TypeScript, because it solve every type issues, but not requires to write code in TypeScript

If it is not possible for you to follow this 2 solutions, please think about using those TS Guidelines. It will reduce your pain in the future.

TypeScript will sync their development within the JavaScript standard. This means there will be no TS Decorators and TypeScript will become more like JS Extension rather than a different language transpiled to JS.

Talk about Types and JS/TS future:

Talk about Types and JS/TS future

ts-guideline's People

Contributors

georgolden avatar jaoodxd avatar paul0lden avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.