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 support #50

Open
saurabhnemade opened this issue Dec 30, 2019 · 8 comments
Open

Typescript support #50

saurabhnemade opened this issue Dec 30, 2019 · 8 comments

Comments

@saurabhnemade
Copy link
Contributor

Can we have types for this?
It would be nice to have for folks with typescript boilerplate.

@giwiro
Copy link
Contributor

giwiro commented Apr 3, 2021

I don't know if it helps but I made one for my project and it works for me.

declare module 'rodal' {
  import {MouseEventHandler, ReactNode} from 'react';

  type RodalProps = {
    children?: ReactNode;
    width?: number;
    height?: number;
    measure?: string;
    visible?: boolean;
    showMask?: boolean;
    closeOnEsc?: boolean;
    closeMaskOnClick?: boolean;
    showCloseButton?: boolean;
    animation?: string;
    enterAnimation?: string;
    leaveAnimation?: string;
    duration?: number;
    className?: string;
    customStyles?: {[key: string]: any};
    customMaskStyles?: {[key: string]: any};
    onClose?: MouseEventHandler<HTMLSpanElement>;
    onAnimationEnd?: () => never;
  };

  const Rodal = (_: RodalProps): JSX.Element => {};
  export = Rodal;
}

@dansmog
Copy link

dansmog commented Nov 23, 2021

How do you make use of this? where do you save it?

@giwiro
Copy link
Contributor

giwiro commented Mar 2, 2022

How do you make use of this? where do you save it?

In order to use custom types you will need to import it through typescript. To do so, add the path to your custom types into the tsconfig.json.
For example if I got the following folder structure (included the custom-types folder):

project/
├── src/
├── custom-types/
    ├── rodal/
    │     └── index.d.ts.        <-- this is where the "decla module 'rodal'" file should be saved
    └── some-other-module/
          └── index.d.ts

In order to add the folder into typescript is adding it to the include key.

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx"
  },
  "include": [
    "src",
    "custom-types/**/*"
  ]
}

@JackMcBride98
Copy link

Make a PR for this :) it helped me out a lot. Although my linter did not like the return type (so I just ignored it for that line)

@Verrok
Copy link

Verrok commented Dec 17, 2022

declare module 'rodal' {
  import {MouseEventHandler, ReactNode} from 'react';

  type RodalProps = {
    children?: ReactNode;
    width?: number;
    height?: number;
    measure?: string;
    visible?: boolean;
    showMask?: boolean;
    closeOnEsc?: boolean;
    closeMaskOnClick?: boolean;
    showCloseButton?: boolean;
    animation?: string;
    enterAnimation?: string;
    leaveAnimation?: string;
    duration?: number;
    className?: string;
    customStyles?: {[key: string]: any};
    customMaskStyles?: {[key: string]: any};
    onClose?: MouseEventHandler<HTMLSpanElement>;
    onAnimationEnd?: () => never;
  };

  const Rodal = (_: RodalProps): JSX.Element => {};
  export = Rodal;
}

thanks a lot

@giwiro
Copy link
Contributor

giwiro commented Jul 15, 2023

Now that I'm using nextjs I changed it a little bit:

declare module 'rodal' {
  import {MouseEventHandler, JSX, PropsWithChildren} from 'react';

  type RodalProps = PropsWithChildren & {
    width?: number;
    height?: number;
    measure?: string;
    visible?: boolean;
    showMask?: boolean;
    closeOnEsc?: boolean;
    closeMaskOnClick?: boolean;
    showCloseButton?: boolean;
    animation?: string;
    enterAnimation?: string;
    leaveAnimation?: string;
    duration?: number;
    className?: string;
    customStyles?: Record<string, any>;
    customMaskStyles?: Record<string, any>;
    onClose?: MouseEventHandler<HTMLSpanElement>;
    onAnimationEnd?: () => never;
  };

  function Rodal(_: RodalProps): JSX.Element;
  export = Rodal;
}

and in the tsconfig.json:

{
  "compilerOptions": {
    "typeRoots": [
      "custom-types",
      "node_modules/@types"
    ],
}

giwiro added a commit to giwiro/rodal that referenced this issue Jul 16, 2023
… be uploaded to the root folder of the package.

It also solves issue chenjiahan#50.
@giwiro
Copy link
Contributor

giwiro commented Jul 16, 2023

I sent a PR #72 for this matter.

@chenjiahan
Copy link
Owner

I sent a PR #72 for this matter.

Thanks for you contribution! This change has been released in v2.1.0

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

No branches or pull requests

6 participants