Skip to content

Convert the whole codebase to TypeScript and make it strict #1735

Closed

Activity

  1. added
    meta
    on Jan 18, 2019
  2. mohamedmansour commented on Jan 31, 2019

    @mohamedmansour

    @sindresorhus why did you name them .tsx file? and not .ts ?

  3. mohamedmansour commented on Jan 31, 2019

    @mohamedmansour

    I Didn't see some files have it, majority do not. Okay :)

  4. nickytonline commented on Jan 31, 2019

    @nickytonline
    Contributor

    I can grab this. I'm pretty comfortable in TypeScript land.

  5. sindresorhus commented on Jan 31, 2019

    @sindresorhus
    MemberAuthor

    @nicksnyder Awesome! Go ahead 🎉

  6. nicksnyder commented on Jan 31, 2019

    @nicksnyder
    Contributor

    Wrong Nick :)

  7. nickytonline commented on Feb 9, 2019

    @nickytonline
    Contributor

    @sindresorhus @bfred-it, I've started the next step to make things strict. I just wanted to check with both of you how you see the strictness being implemented.

    There's lots of examples in the codebase that always assumes that something exists, e.g.

    const commentedNode = el.parentNode.nextSibling as Text;
    

    There are a few ways to approach this:

    // Short and sweet with the non-null assertion operator
    const commentedNode = el!.parentNode!.nextSibling as Text;
    

    or

    // Can kill readbility
    const commentedNode = (el as Element).(parentNode as Element).nextSibling as Text;
    

    or

    // Lots of boileplate for existence checks but better guarantees
    if (!(el.parentNode && el.parentNode.nextsibling)) {
    	return;
    }
    
    const commentedNode = el.parentNode.nextSibling as Text;
    
    • The first way reads well and we just tell TS we know it exists
    • Second way is potentially harder to read if properties are deeply nested and we just tell TS we know it exists.
    • Third way can get more verbose with all the existence checks, but is more robust.

    I try not to cast too much or use ! too often, but ! can have value in terms of better readability especially in a case like RGH where a lot of DOM elements are assumed to exist.

    The other thing is as soon as strict mode is enabled, it complains about JSX. From what I can tell, the only way to type it so the compiler doesn't complain is to cast, e.g.

    ((<svg aria-hidden="true" className="octicon octicon-check" width="12" height="16"><path d="M12 5l-8 8-4-4 1.5-1.5L4 10l6.5-6.5z"/></svg> as unknown) as SVGElement);
    

    Also, dom-chef has no types and since it's React like in terms of JSX, I brought in the @types/react package. The other thing it complains about is all the class attributes on the JSX. dom-chef supports className, so I'd suggest going with that.

    The above double casting can become a lot of copy paste as there is a lot of JSX in the project. One other option could be an identity function that just returns it cast properly. It means a useless function to just get the right type, but it may read better. Just a suggestion.

    function jsxToElementType<TElement extends Element = Element>(jsx: unknown) {
    	return jsx as TElement;
    }
    

    Let me know what you think. I'm going to catch some 💤

  8. fregante commented on Feb 9, 2019

    @fregante
    Member

    Thanks for checking first! Honestly I don't like any of that. Can we avoid the strictness altogether?

    Sometimes we need to check each element (e.g. if !el, return) because the page filter (e.g. isRepo) isn't as granular as needed; most of the times though we need it to fail and error out if the DOM doesn't match our expectations, so someone will notice quicker and it will be fixed, instead of silently failing.

  9. nickytonline commented on Feb 10, 2019

    @nickytonline
    Contributor

    We can avoid the strictness altogether, it's just that you can't enable strict mode then (one of the criteria in this issue). So if you still want to enable strict mode, I'd suggest using the non-null assertion operator, !, based on your comments.

    most of the times though we need it to fail and error out if the DOM doesn't match our expectations, so someone will notice quicker and it will be fixed, instead of silently failing.

  10. nickytonline commented on Feb 13, 2019

    @nickytonline
    Contributor

    @bfred-it, just following up on this. Are we just not going to enable strict mode then?

  11. fregante commented on Feb 13, 2019

    @fregante
    Member
  12. fregante commented on Feb 13, 2019

    @fregante
    Member

    Perhaps open a PR with what you have

  13. sindresorhus commented on Feb 13, 2019

    @sindresorhus
    MemberAuthor

    @bfred-it The ! operator fixes most of these annoyances. I use it heavily when I know it's not going to be null. Strictness doesn't have to be all or nothing either. We can choose to enable just some of the strictness flags.

  14. hardikmodha commented on Apr 3, 2019

    @hardikmodha
    Contributor

    Is anybody still working on this? I would more than happy to pick this up.

  15. sindresorhus commented on Apr 3, 2019

    @sindresorhus
    MemberAuthor

    @hardikmodha It's being worked on in #1783.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    acceptedmeta

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions