Skip to content

Use import type for types instead of import #607

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

Closed
wants to merge 9 commits into from
Closed

Use import type for types instead of import #607

wants to merge 9 commits into from

Conversation

DeepDoge
Copy link
Contributor

Made it generate type imports with import type instead of import alone.

What I changed:

  • Before createImportDeclaration and createImportTypeDeclaration functions were both doing the exact same thing, now createImportTypeDeclaration imports with import type.
  • When createImportsForUsedIdentifiers used, if the moduleSpecifier is starting with type, it's using createImportTypeDeclaration for it instead of createImportDeclaration.

I did this because this is how it's suppose to be and it was causing problems with Svelte's Vite.

Copy link
Member

@krzkaczor krzkaczor left a comment

Choose a reason for hiding this comment

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

Thanks for this PR! 🙇

Personally, I don't use import/export type that much but I get that some tooling might require it. The problem with this PR is that right now our tests can't catch any mistakes related to this. I can change import to import type and the other way around and CI still passes.

How can we verify that current changes work?

'BigNumberish',
],
'@ethersproject/providers': ['Provider', 'TransactionRequest'],
ethers: ['Signer', 'utils', 'Contract', 'ContractFactory', 'PayableOverrides', 'BytesLike', 'BigNumberish'],
Copy link
Member

Choose a reason for hiding this comment

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

I can change this line to:

Suggested change
ethers: ['Signer', 'utils', 'Contract', 'ContractFactory', 'PayableOverrides', 'BytesLike', 'BigNumberish'],
'type ethers': ['Signer', 'utils', 'Contract', 'ContractFactory', 'PayableOverrides', 'BytesLike', 'BigNumberish'],

(merge with next line) and all tests still pass.

@DeepDoge
Copy link
Contributor Author

Do you also want me to edit tests so when something that is a type imported without import type or the other way around, we catch it in the tests?

@changeset-bot
Copy link

changeset-bot bot commented Jan 29, 2022

⚠️ No Changeset found

Latest commit: a8554a4

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@DeepDoge
Copy link
Contributor Author

DeepDoge commented Jan 29, 2022

@krzkaczor

  • Added "importsNotUsedAsValues": "error" in tsconfig in the root.
    • This should make the typecheck fail if there is any type imported without import type and haven't been used as value inside the file.
  • And also updated all of the files in the project accordingly.

@@ -0,0 +1,6 @@
/** @type {import("eslint").Linter.Config} */
Copy link
Member

Choose a reason for hiding this comment

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

Is this file needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I realized that it was giving ide errors because the folder had files with console.logs but the root eslint config doesn't allow console.logs.

But scripts folder contains files for cli scripts.

So i suppose it's needed.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm but this way this file is also not used by anything.... I think the real solution would be to enforce root eslint config for every single file (right now it's only for code that is actually published) and then ignore valid console.logs with eslint ignore comments. I'd remove this from this PR and do this in a separate one.

tsconfig.json Outdated
@@ -9,7 +9,8 @@
"sourceMap": true,
"composite": true,
"incremental": true,
"exactOptionalPropertyTypes": true
"exactOptionalPropertyTypes": true,
"importsNotUsedAsValues": "error"
Copy link
Member

Choose a reason for hiding this comment

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

what if we enforce this only in packages/target-ethers-v5/tsconfig.json? I don't feel that it's necessary to use accross the whole codebase.

@krzkaczor
Copy link
Member

@DeepDoge thanks! This flag importsNotUsedAsValues is what we need (i think). It just should be scoped to ethers test package. I hope there is automatic way to fix it 😅

@DeepDoge
Copy link
Contributor Author

DeepDoge commented Jan 30, 2022

@krzkaczor I mean, I believe it would be good that if we use it for the whole codebase. It's more consistent and I already updated the whole codebase for it.
Why it would be good because for example while im running the tests, i also realized that other than ethers v5, ethers v4 and web3 had the same problem too. And I fixed those too.
I didn't realized those before because i was only using ethers5 in my project.

But if you really don't want it for the whole codebase, I can only scope it for, ethers v5, v4 and web3 tests.

EDIT: Left for my military service, brb next month.

@krzkaczor
Copy link
Member

krzkaczor commented Feb 3, 2022

But if you really don't want it for the whole codebase, I can only scope it for, ethers v5, v4 and web3 tests.

Yes, I would prefer to do it like this. I am not sure if there is an automatic way to enforce proper use of import type and I don't want to put the additional burden on maintainers/contributors.

EDIT: Left for my military service, brb next month.

Good luck! :)

@DeepDoge
Copy link
Contributor Author

DeepDoge commented Mar 7, 2022

@krzkaczor can you check now?

@@ -20,8 +20,12 @@ describe('TypeChain x Hardhat', () => {
const artifact = require('../artifacts/contracts/Counter.sol/Counter.json') as { abi: Interface; bytecode: string }

const counterFactory1 = new Counter__factory()
counterFactory1
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this needed?

@krzkaczor
Copy link
Member

Superseded by #636

@krzkaczor
Copy link
Member

Thanks, @DeepDoge for your work on this 🙇 I merged it as #636.

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.

3 participants