Refined-github: Convert the whole codebase to TypeScript and make it strict

Created on 18 Jan 2019  路  17Comments  路  Source: sindresorhus/refined-github

Most helpful comment

Wrong Nick :)

All 17 comments

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

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

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

@nicksnyder Awesome! Go ahead 馃帀

Wrong Nick :)

I can help too.
where should I start?

@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 馃挙

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.

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.

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

@sindresorhus

Perhaps open a PR with what you have

@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.

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

@HardikModha It's being worked on in https://github.com/sindresorhus/refined-github/pull/1783.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

shivapoudel picture shivapoudel  路  3Comments

hkdobrev picture hkdobrev  路  3Comments

juliocanares picture juliocanares  路  3Comments

pawelad picture pawelad  路  3Comments

yakov116 picture yakov116  路  3Comments