Skip to content

Add TypeScript definitions #146

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

Merged
merged 10 commits into from
Jun 3, 2023
Merged

Add TypeScript definitions #146

merged 10 commits into from
Jun 3, 2023

Conversation

aknuds1
Copy link
Owner

@aknuds1 aknuds1 commented Apr 7, 2023

Cherry-pick of #142, since that has a merge conflict. Thanks @xnevz!

Since the ones from #142 don't work properly, TS type definitions (in the types directory) are automatically generated, by adding the following tsconfig.json and executing tsc:

{
  "include": ["lib/**/*"],
  "compilerOptions": {
    "allowJs": true,
    // Generate d.ts files
    "declaration": true,
    "emitDeclarationOnly": true,
    "outDir": "types",
    "declarationMap": true,
    "strict": true
  }
}

To test that the type definitions are valid, I've also rewritten the test suite in TypeScript and checked in VS Code.

Fixes #134.

@coveralls
Copy link

Coverage Status

Coverage: 100.0%. Remained the same when pulling 192329e on add-ts-definitions into dceacca on master.

@coveralls
Copy link

coveralls commented Apr 7, 2023

Coverage Status

coverage: 100.0%. remained the same when pulling 41915bf on add-ts-definitions into 30ab49f on master.

@aknuds1
Copy link
Owner Author

aknuds1 commented Apr 7, 2023

@emlynmac do you want to review the TS definitions? I don't really know TS myself.

@aknuds1 aknuds1 force-pushed the add-ts-definitions branch 2 times, most recently from ee75100 to 4f92cbf Compare April 7, 2023 08:53
@aknuds1 aknuds1 mentioned this pull request Apr 7, 2023
@xnevz
Copy link

xnevz commented Apr 7, 2023

@aknuds1 happy to be of help !

@aknuds1 aknuds1 force-pushed the add-ts-definitions branch from 4f92cbf to 75d6677 Compare April 7, 2023 16:17
@aknuds1
Copy link
Owner Author

aknuds1 commented Apr 7, 2023

It looks to me that the TS definitions are broken, any help would be appreciated:

./node_modules/.bin/tsc
lib/camel-case-attribute-names.d.ts:1:1 - error TS1046: Top-level declarations in .d.ts files must start with either a 'declare' or 'export' modifier.

1 const camelCaseMap: { [id: string]: string; };
  ~~~~~

lib/parser.d.ts:1:10 - error TS2614: Module '"./processing-instructions"' has no exported member 'ProcessingInstructions'. Did you mean to use 'import ProcessingInstructions from "./processing-instructions"' instead?

1 import { ProcessingInstructions } from './processing-instructions';
           ~~~~~~~~~~~~~~~~~~~~~~

lib/parser.d.ts:7:2 - error TS1036: Statements are not allowed in ambient contexts.

7 };
   ~

lib/parser.d.ts:11:30 - error TS2552: Cannot find name 'ReactElement'. Did you mean 'SVGRectElement'?

11     parse: (html: string) => ReactElement;
                                ~~~~~~~~~~~~

  node_modules/typescript/lib/lib.dom.d.ts:13312:13
    13312 declare var SVGRectElement: {
                      ~~~~~~~~~~~~~~
    'SVGRectElement' is declared here.

lib/processing-instructions.d.ts:2:10 - error TS2614: Module '"./process-node-definitions"' has no exported member 'processDefaultNode'. Did you mean to use 'import processDefaultNode from "./process-node-definitions"' instead?

2 import { processDefaultNode } from "./process-node-definitions";
           ~~~~~~~~~~~~~~~~~~


Found 5 errors in 3 files.

@aknuds1
Copy link
Owner Author

aknuds1 commented Apr 8, 2023

Since I found that the TS type definitions from #142 wouldn't compile, I've committed and pushed automatically generated type definitions in the types/ directory instead. I generated them by adding the following tsconfig.json and executing tsc:

{
  "include": ["lib/**/*"],
  "compilerOptions": {
    "allowJs": true,
    // Generate d.ts files
    "declaration": true,
    "emitDeclarationOnly": true,
    "outDir": "types",
    "declarationMap": true,
    "strict": true
  }
}

To test that the type definitions are valid, I've also rewritten the test suite in TypeScript and checked in VS Code.

@emlynmac
Copy link

The auto-generated TS definitions are a little weak - the definitions are basically using any which doesn't have any type-checking.
I think this week I'm not gonna have time to look at this properly, but hopefully next I can find some time to go over this more thoroughly.

@aknuds1
Copy link
Owner Author

aknuds1 commented Apr 11, 2023

Thank you @emlynmac - that would be great.

@emlynmac
Copy link

emlynmac commented Apr 12, 2023

I got a chance to look at this today and have an overlay type definition for local use:

html-to-react.d.ts

declare module 'html-to-react' {
  import { ParserOptions } from 'htmlparser2';
  import { ReactElement } from 'react';

  function Html2ReactParser(options?: ParserOptions): {
    parse: (html: string) => JSX.Element;
    parseWithInstructions: (
      html: string,
      isValidNode: (node: any) => boolean,
      processingInstructions?: ProcessingInstructionType[],
      preprocessingInstructions?: ProcessingInstructionType[]
    ) => JSX.Element;
  };

  export type ProcessNodeFunctionType = (node: any, children: any, index: number) => ReactElement;

  export type ProcessingInstructionType = {
    shouldProcessNode: (node: any) => boolean;
    processNode: ProcessNodeFunctionType;
  };

  function ProcessingInstructions(): {
    defaultProcessingInstructions: ProcessingInstructionType;
  };

  function ProcessNodeDefinitions(): {
    processDefaultNode: ProcessNodeFunctionType;
  };

  const IsValidNodeDefinitions: {
    alwaysValid(): boolean;
  };

  export { Html2ReactParser as Parser, ProcessingInstructions, IsValidNodeDefinitions, ProcessNodeDefinitions };
}

@emlynmac
Copy link

I added #148 to cover the version I put together.

@emlynmac emlynmac mentioned this pull request Apr 17, 2023
@aknuds1 aknuds1 force-pushed the add-ts-definitions branch from 555d90a to 4373412 Compare April 23, 2023 16:58
@aknuds1 aknuds1 force-pushed the add-ts-definitions branch 2 times, most recently from 75c7401 to 5161f2b Compare June 3, 2023 12:34
xnevz and others added 7 commits June 3, 2023 14:35
Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
aknuds1 added 3 commits June 3, 2023 14:35
Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
@aknuds1 aknuds1 force-pushed the add-ts-definitions branch from 5161f2b to 41915bf Compare June 3, 2023 12:36
@aknuds1 aknuds1 merged commit a2721e4 into master Jun 3, 2023
@aknuds1 aknuds1 deleted the add-ts-definitions branch June 3, 2023 12:44
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.

Add TypeScript declarations
4 participants