Skip to content
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

Convert extend util to TypeScript #2928

Merged
merged 18 commits into from
Nov 8, 2021
Merged

Conversation

dsevillamartin
Copy link
Member

@dsevillamartin dsevillamartin commented Jun 20, 2021

Changes proposed in this pull request:

  • Use .ts extension when referring to extend util in core code
  • Allow extensions to use .ts in import path through compat for typings to work properly
  • Convert extend util to TypeScript

Reviewers should focus on:

  • Do we want to create a bunch more exported types/interfaces to make reading the code easier
  • I don't know how to make it so the typings accept a nonexistent key for extend
  • Are we fine with the new RegExp for the compat proxy to accept .js, .ts and .tsx

Screenshot
image

Confirmed

  • Frontend changes: tested on a local Flarum installation.

@davwheat
Copy link
Member

Out of curiosity, what's the reasoning behind stating the file extension? Is it just to fix typings so that they work properly?

@dsevillamartin
Copy link
Member Author

@davwheat Yes. We currently have an extend.js and an extend/index.js, and so no file extension resolves to extend/index.js.

@dsevillamartin
Copy link
Member Author

dsevillamartin commented Jun 20, 2021

Currently fixing issues with my proxifyCompat changes, as they break imports of e.g. tags, and others.

EDIT: ^ Fixed

@SychO9 SychO9 added this to the 1.1 milestone Jun 27, 2021
@dsevillamartin
Copy link
Member Author

what happened here

@davwheat
Copy link
Member

davwheat commented Aug 18, 2021

Rebased onto master

js/src/common/Application.js Outdated Show resolved Hide resolved
js/src/common/extend.ts Outdated Show resolved Hide resolved
js/src/common/extend.ts Outdated Show resolved Hide resolved
js/src/common/extend.ts Show resolved Hide resolved
js/src/common/extend.ts Show resolved Hide resolved
js/src/common/utils/proxifyCompat.ts Outdated Show resolved Hide resolved
js/src/common/extend.ts Outdated Show resolved Hide resolved
js/src/common/extend.ts Outdated Show resolved Hide resolved
js/src/common/extend.ts Show resolved Hide resolved
@davwheat
Copy link
Member

Do we want to create a bunch more exported types

I'd say that for something like this (utility types), we should declare them in the @types folder we now have. That lets us use them across core without imports, and extensions can use them too (from 1.0.5 now we have the fix for global typings).

Copy link
Member

@askvortsov1 askvortsov1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, let's run prettier and revert the regex change, then good to go!

@SychO9
Copy link
Member

SychO9 commented Aug 20, 2021

(from 1.0.5 now we have the fix for global typings).

1.1 actually, since it was merged into master and not 1.0.5 unless you want to cherry pick it.

I am not well informed to know if it's something that needs to be in 1.0.5 and not wait 1.1

@davwheat
Copy link
Member

Oops. I'd say we should stick it in 1.0.5 if we can. It's just a developer improvement and waiting a whole release cycle to provide it seems pointless.

@askvortsov1
Copy link
Member

Oops. I'd say we should stick it in 1.0.5 if we can. It's just a developer improvement and waiting a whole release cycle to provide it seems pointless.

I would prefer to keep patch releases focused on bugfixes tbh.

@davwheat davwheat requested review from davwheat and removed request for davwheat August 20, 2021 20:09
Copy link
Member

@davwheat davwheat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to dismiss my review as I've contributed a fair bit to this PR.

@askvortsov1
Copy link
Member

I'm going to dismiss my review as I've contributed a fair bit to this PR.

I don't believe that's necessary. That being said, @datitisev could you confirm that you approve of the changes?

@dsevillamartin
Copy link
Member Author

@askvortsov1 I... think? I mean now I understand the KeyOfType types even less but I didn't understand them before so that doesn't matter. The issue of using override with non-declared properties still exists, not sure if we wanted to fix that or not. Doesn't really matter as we allow building with TS errors.

image

@askvortsov1
Copy link
Member

The issue of using override with non-declared properties still exists

In those cases, wouldn't we want to modify the prototype directly?

@dsevillamartin
Copy link
Member Author

@askvortsov1 Not if it has the potential to exist but its optional? Cause then you can take advantage of the override... I think my main example was extend(options, 'config') or something in app.request.

@askvortsov1
Copy link
Member

@askvortsov1 Not if it has the potential to exist but its optional? Cause then you can take advantage of the override... I think my main example was extend(options, 'config') or something in app.request.

Wdym by "take advantage of the override"? Are there any advantages compared to directly editing the prototype?

@dsevillamartin
Copy link
Member Author

dsevillamartin commented Aug 20, 2021

@askvortsov1 Oh I'm stupid, I'm thinking about extend() and not override()

EDIT: wait no i did say extend, i think i need sleep... what is going on
EDIT 2: oh i mentioned both 🤦

In app.request

    extend(options, 'config', (result, xhr) => xhr.setRequestHeader('X-CSRF-Token', this.session.csrfToken));

Here it would be useful because we account for it not existing. My apologies.

image

@davwheat
Copy link
Member

I think if we allow | string, we still benefit from the Intellisense of the KeysOfType<> type? Don't quote me on that tho...

@dsevillamartin
Copy link
Member Author

Maybe we should add a separate util for that then? A guaruntee that the extended/overriden method exists is pretty useful generally.

This'd be breaking then tho. And I honestly don't see a problem with the current implementation 🤷‍♂️

@askvortsov1
Copy link
Member

Maybe we should add a separate util for that then? A guaruntee that the extended/overriden method exists is pretty useful generally.

This'd be breaking then tho. And I honestly don't see a problem with the current implementation 🤷‍♂️

Not really, as we'd be changing the type interface, not the implementation.

Its not that there's a problem with the current non-restrictive approach as much as we're losing out on the ability to enforce something statically through the type system: in particular, because defining a function as a composition of other functions isn't quite the same as extending an existing function.

@tankerkiller125 tankerkiller125 removed their request for review August 22, 2021 11:27
@askvortsov1 askvortsov1 modified the milestones: 1.1, 1.2 Sep 28, 2021
@davwheat
Copy link
Member

I think this is fine as-is. The highlighted issue, if anything, will likely help people as they may realise they're extending the wrong thing (if it doesn't exist), rather than failing silently.

If needed, you can use // @ts-expect-error to override TS until we have another way to do it.

@dsevillamartin dsevillamartin merged commit b90001d into master Nov 8, 2021
@dsevillamartin dsevillamartin deleted the ds/util-extend-typings branch November 8, 2021 21:52
@davwheat davwheat mentioned this pull request Dec 20, 2021
8 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants