•  
      request #14198 Disallow type assertions in production code
    Infos
    #14198
    Joris MASSON (jmasson)
    2019-11-20 17:17
    2019-11-20 11:59
    15459
    Details
    Disallow type assertions in production code

    Quoting from @typescript-eslint 's doc:

    Always prefer const x: T = { ... }; to const x = { ... } as T; (or similar with angle brackets). The type assertion in the latter case is either unnecessary or will probably hide an error.

    The compiler will warn for excess properties with this syntax, but not missing required fields. For example: const x: { foo: number } = {}; will fail to compile, but const x = {} as { foo: number } will succeed.

    We can allow "as Type" type assertions in unit tests because it greatly simplifies setup and readability, but we should raise errors in production code.

    There are however a few corner cases where type assertions can be used in production code. Either our dependencies (Vuex for example) have bad typing support, or we are referencing a special kind of DOM element (for example HTMLElement) using this.$refs in Vue and Vue has no way of knowing that.

    Dev tools
    Empty
    Empty
    • [ ] enhancement
    • [x] internal improvement
    Empty
    Stage
    Empty
    Closed
    2019-11-20
    Attachments
    Empty
    References

    Follow-ups