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

TypeScript rewrite #441

Merged
merged 28 commits into from
Mar 10, 2019
Merged

TypeScript rewrite #441

merged 28 commits into from
Mar 10, 2019

Conversation

atomiks
Copy link
Owner

@atomiks atomiks commented Mar 8, 2019

Resolves #378

cc @KubaJastrz, also how do I use a single declaration file (./types.ts and index.d.ts are duplicates?)

@atomiks atomiks requested a review from KubaJastrz March 8, 2019 07:44
@KubaJastrz
Copy link
Collaborator

Definitely a good start. There are still many as and any keywords that could be replaced. And .eslintrc is not set up correctly for typescript plugin.

I'm gonna do a full review later today locally and check what can be written more strictly.

@atomiks
Copy link
Owner Author

atomiks commented Mar 8, 2019

One thing that really confuses me is the angle bracket syntax <>. I don't understand it at all. I read the docs for it and it's meant to be like as(?) but I don't get it, yet see it everywhere 🤯

@KubaJastrz
Copy link
Collaborator

KubaJastrz commented Mar 8, 2019

They can be used for type casting, but only in .ts files (that is not the case for .tsx in React), so I recommend not using it this way at all (just use as keyword instead).

Another use that you might see (basically all the time) is as generic type. It works sort of like a function parameter. More info here.

@atomiks
Copy link
Owner Author

atomiks commented Mar 8, 2019

Yeah I thought the generic type would be good for the util functions but seemed to cause problems so just fell back to any :. Anyway look forward to your improvements 🙂

Copy link
Collaborator

@KubaJastrz KubaJastrz left a comment

Choose a reason for hiding this comment

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

That was quite a ride. Probably missed a few things 😅
Good job 👍

.eslintrc Show resolved Hide resolved
tsconfig.json Outdated Show resolved Hide resolved
src/createTippy.ts Outdated Show resolved Hide resolved
src/createTippy.ts Outdated Show resolved Hide resolved
src/createTippy.ts Outdated Show resolved Hide resolved
src/ponyfills.ts Outdated Show resolved Hide resolved
src/utils.ts Outdated Show resolved Hide resolved
src/popper.ts Outdated Show resolved Hide resolved
src/popper.ts Outdated Show resolved Hide resolved
src/reference.ts Outdated Show resolved Hide resolved
@atomiks
Copy link
Owner Author

atomiks commented Mar 9, 2019

Thank you tons for the help @KubaJastrz ! I think we've caught a few obscure bugs thanks to TS 😎

It seems like there are lots of these errors now:

[ts] Cannot invoke an object which is possibly 'undefined'. [2722]

[ts] Object is possibly 'null'. [2531]

[ts] Object is possibly 'undefined'

With regards to the instance props, etc. Any ideas of what to do?

@atomiks
Copy link
Owner Author

atomiks commented Mar 9, 2019

I think we need to distinguish the Props interface and Options

Like this?

export interface Props {
  [all required, no ?:]
}

export type Options = Partial<Props>

That would probably be a breaking change for the types though...? :/

@atomiks
Copy link
Owner Author

atomiks commented Mar 9, 2019

Managed to get all types passing 🤷‍♂️

Had to hack the EventListener type for the on* trigger listeners. I'm sure there's a better way

Copy link
Collaborator

@KubaJastrz KubaJastrz left a comment

Choose a reason for hiding this comment

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

There are still some Function types to remove.

We could drop index.d.ts and replace it with --declaration flag during the build process. I'll look into it.

src/createTippy.ts Outdated Show resolved Hide resolved
src/createTippy.ts Outdated Show resolved Hide resolved
src/popper.ts Outdated Show resolved Hide resolved
src/popper.ts Outdated Show resolved Hide resolved
src/popper.ts Outdated Show resolved Hide resolved
tsconfig.json Outdated Show resolved Hide resolved
@atomiks
Copy link
Owner Author

atomiks commented Mar 9, 2019

We could drop index.d.ts and replace it with --declaration flag during the build process. I'll look into it.

We need to keep the Props backwards-compat though. So I think we should keep it separate for this major?

@KubaJastrz
Copy link
Collaborator

There's something weird going on. We have our internal types in src/types.ts. That's where I proposed to create Defaults interface that just mirrors the object in src/defaults.ts.

So the flow would be something like this:

// in src/types.ts
import defaults from './defaults'
type Defaults = typeof defaults
interface Props extends Defaults /* maybe omit some props */ {
  /* and override them here, like we do in @tippy.js/react */
}

No need for Options interface.

Because we're using typescript, we could potentially drop index.d.ts and have it be generated automatically during build process (I hope this can be done without breaking changes, not sure).

@atomiks
Copy link
Owner Author

atomiks commented Mar 9, 2019

Well ideally we don't have a Defaults interface. I only added it as part of index.d.ts backwards compat. We should rename that file I guess and move props.ts stuff into utils.ts.

There should be two interfaces: Props and Options = Partial<Props>

@atomiks
Copy link
Owner Author

atomiks commented Mar 9, 2019

@KubaJastrz I think this is pretty decent now? I'll keep index.d.ts and ./types.ts separate for now, we can work out what to do in next major. Currently they're synced with some internal/private types taken away.

@KubaJastrz
Copy link
Collaborator

KubaJastrz commented Mar 9, 2019

Well ideally we don't have a Defaults interface.

So why do we have it now? I thought that could be used for fixing this, but currently it's just a copy of previous Props interface (without ?).

@atomiks
Copy link
Owner Author

atomiks commented Mar 10, 2019

So why do we have it now? I thought that could be used for fixing this, but currently it's just a copy of previous Props interface (without ?).

Cuz I copy-pasted the Props interface from ./types which included a couple fixes 😛 Will be easier to keep them in sync if I don't need to convert them to be optional in index.d.ts

Moving forward, people should import Options instead of Props to represent the optional interface which will work with this major. I'll rename the merged / full props interface to WholeProps for now in index.d.ts? People seemed to work fine without a full Props interface anyway before.

@atomiks atomiks closed this Mar 10, 2019
@atomiks atomiks reopened this Mar 10, 2019
@atomiks
Copy link
Owner Author

atomiks commented Mar 10, 2019

Going to merge this in now as linting, types and tests pass fine, I'll do a minor release in the next week sometime. Feel free to make some PRs in the meantime to improve the current types 🙂

@atomiks atomiks merged commit dce36f8 into master Mar 10, 2019
@atomiks atomiks deleted the typescript-rewrite branch March 10, 2019 09:46
@KubaJastrz
Copy link
Collaborator

I don't know what WholeProps would be useful for. I think it's unnecessary because you can easily use something like this in the userland :

type WholeProps = Required<Options>

I suggest just sticking to Props -> Options rename if you really want it.

@atomiks
Copy link
Owner Author

atomiks commented Mar 10, 2019

It types instance.props (default props + user-supplied options), but other interface use Options (only user-supplied options).

People were doing #408;

import { Props } from 'tippy.js'

But it will break if we change Props to be Required<Options> (or use non-optional interface)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants