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

Feat: Add typescript types for @faker-js/faker #9

Closed
damienwebdev opened this issue Jan 7, 2022 · 22 comments
Closed

Feat: Add typescript types for @faker-js/faker #9

damienwebdev opened this issue Jan 7, 2022 · 22 comments
Labels
c: feature Request for new feature

Comments

@damienwebdev
Copy link
Member

No description provided.

@damienwebdev damienwebdev changed the title Feat: Add typescript types for @graycore/faker Feat: Add typescript types for @faker-js/faker Jan 7, 2022
@Shinigami92 Shinigami92 added the c: feature Request for new feature label Jan 9, 2022
@nick-krantz
Copy link

I'd be interested in contributing to getting types going for the project. Based on the discussion in #14, I think the intention is to add the types to DefinitelyTyped? Is that correct?

@Shinigami92
Copy link
Member

For now please create a file in your repo with content:

// faker.d.ts
declare module '@faker-js/faker' {
  import faker from 'faker';
  export default faker;
}

and then just use the @types/faker

@Shinigami92
Copy link
Member

The rewrite to TS could take some days

@damienwebdev
Copy link
Member Author

read: weeks.

@nick-krantz
Copy link

Oh I must have misunderstood, I thought there would be an intermediate state where types would come before the TS rewrite.

read: weeks.

I believe it

@Shinigami92
Copy link
Member

Shinigami92 commented Jan 10, 2022

// faker.d.ts
declare module '@faker-js/faker' {
  import faker from 'faker';
  export default faker;
}

It works! I tested this im my own repo at work!
Nothing wrong with them, cause until now we did not change anything according to that.

@sramam
Copy link

sramam commented Jan 10, 2022

Can't this be actually included in the current repo?
That way, it'll get fixed when it'll get fixed and we won't have to back out this change from a 10^6 downstream modules.

Happy to submit a PR is that seems like an acceptable band-aid.

@MilosPaunovic
Copy link
Member

Hello @MohdImran001,

You were willing to help with types on community-faker, https://github.com/MilosPaunovic/community-faker/issues/3, but, for the benefit of the OpenSource, we are moving the official community-driven fork here, so, are you willing to help out here, as well?

@Shinigami92
Copy link
Member

Can't this be actually included in the current repo? That way, it'll get fixed when it'll get fixed and we won't have to back out this change from a 10^6 downstream modules.

Happy to submit a PR is that seems like an acceptable band-aid.

This will not work, cause the current versions <=5.5.3 are already released and not touched by us except the package name.
But I already working on TS migration at all #72, but sadly I'm a human and not a AI, so this could take a week or so

@MohdImran001
Copy link
Contributor

Thanks, for tagging me here @MilosPaunovic. 😃. As far as I have understood, @Shinigami92 is working on all the Typescript related things in #72. @Shinigami92 Please let me know, how can I get started. I have joined the discord server.

@damienwebdev
Copy link
Member Author

Given the workaround here: #9 (comment) I'm closing this issue.

@nicolasrouanne
Copy link

nicolasrouanne commented Jan 13, 2022

FWIW in order to use named imports, I had to make a slightly different export from the .d.ts file than the proposed solution:

// faker.d.ts
declare module '@faker-js/faker' {
  import faker from 'faker';
  export = faker;
}

Using it as named imports

// some test file
import { datatype, date, hacker } from '@faker-js/faker';

This was inspired by how faker is exported from DefinitelyTyped

@micalevisk
Copy link

another way is using TS path aliases like:

{
  "compilerOptions": {
    "paths": {
      "faker/*": ["@faker-js/faker/*"],
      "faker": ["@faker-js/faker"]
    }
  }
}

thus you don't need to change anything besides the tsconfig file.

@SametSahin10
Copy link

For now please create a file in your repo with content:

// faker.d.ts
declare module '@faker-js/faker' {
  import faker from 'faker';
  export default faker;
}

and then just use the @types/faker

Does the location of the faker.d.ts file matter? Where should we put the file?

@Shinigami92
Copy link
Member

Does the location of the faker.d.ts file matter? Where should we put the file?

Please note that you can already switch to v6.0.0-alpha.3 with native TS support 🙌

@SametSahin10
Copy link

Does the location of the faker.d.ts file matter? Where should we put the file?

Please note that you can already switch to v6.0.0-alpha.3 with native TS support raised_hands

Oh, really? I spent some time trying to figure out how I can link the faker.d.ts with the @types/faker package. Maybe the README file could be updated to remove the Typescript Support section or there could be a mention of the fact that TS support is available in the latest version.

@Shinigami92
Copy link
Member

Oh, really? I spent some time trying to figure out how I can link the faker.d.ts with the @types/faker package. Maybe the README file could be updated to remove the Typescript Support section or there could be a mention of the fact that TS support is available in the latest version.

Already planed 😉
But we decide it to leave as is for now, until v6.0.0 is fully released

@SametSahin10
Copy link

The index.d.ts file handles the linking, right? Back then, we needed to create the faker.d.ts file because the index.d.ts did not exist. I'm currently learning how declaration files work in TS. @Shinigami92

@Shinigami92
Copy link
Member

The index.d.ts file handles the linking, right? Back then, we needed to create the faker.d.ts file because the index.d.ts did not exist. I'm currently learning how declaration files work in TS. @Shinigami92

Yes it does, sadly we have currently for some an issue in the -alpha.3 because we shipped the index.ts next to index.d.ts, but this will be already fixed in -alpha.4

@SametSahin10
Copy link

The index.d.ts file handles the linking, right? Back then, we needed to create the faker.d.ts file because the index.d.ts did not exist. I'm currently learning how declaration files work in TS. @Shinigami92

Yes it does, sadly we have currently for some an issue in the -alpha.3 because we shipped the index.ts next to index.d.ts, but this will be already fixed in -alpha.4

Is the issue about importing? Cause I cannot import faker with:

import * as faker from "faker"

I get "Cannot find module or its type declarations" and "Unable to resolve path to module faker".

@Shinigami92
Copy link
Member

@SametSahin10 Please try import faker from "@faker-js/faker" 😉

Could also be that you need "esModuleInterop": true in tsconfig.json

@SametSahin10
Copy link

SametSahin10 commented Jan 18, 2022

@SametSahin10 Please try import faker from "@faker-js/faker" wink

Could also be that you need "esModuleInterop": true in tsconfig.json

I've added "esModuleInterop": true into tsconfig.json and it worked. 👍

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

No branches or pull requests

9 participants