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

Cannot import since update to version 6.0.0 #87

Closed
Farbfetzen opened this issue Aug 15, 2023 · 6 comments
Closed

Cannot import since update to version 6.0.0 #87

Farbfetzen opened this issue Aug 15, 2023 · 6 comments

Comments

@Farbfetzen
Copy link

Hello, I am using ansi_up in an Angular app and today I wanted to update to version 6.0.0. But after the update I get this error:

Could not find a declaration file for module 'ansi_up'. '<redacted>/node_modules/ansi_up/ansi_up.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/ansi_up` if it exists or add a new declaration (.d.ts) file containing `declare module 'ansi_up';`ts(7016)

I am importing it like this: import AnsiUp from 'ansi_up'; which has worked fine until now. I don't exactly understand what went wrong here. Somehow it tries to import the .js file instead of the .ts file. Is there a different way I could try to import it?

@drudru
Copy link
Owner

drudru commented Aug 15, 2023

Version 6 is an api change.
It is now an ES6 module. Check the readme for examples of the new import style.
Also, if you are in a hurry, just lock to version 5.2.1

let me know how it goes

@Farbfetzen
Copy link
Author

Thank you for your help. For the time being I will stick to the previous version.
I changed the import to import { AnsiUp } from 'ansi_up/ansi_up.js';. Now it seems that the correct files are found, however, I get these typescript compiler errors:

Error: node_modules/ansi_up/ansi_up.ts:64:13 - error TS2564: Property 'ansi_colors' has no initializer and is not definitely assigned in the constructor.

64     private ansi_colors:AU_Color[][];
               ~~~~~~~~~~~


Error: node_modules/ansi_up/ansi_up.ts:65:13 - error TS2564: Property 'palette_256' has no initializer and is not definitely assigned in the constructor.

65     private palette_256:AU_Color[];
               ~~~~~~~~~~~


Error: node_modules/ansi_up/ansi_up.ts:75:13 - error TS2564: Property '_csi_regex' has no initializer and is not definitely assigned in the constructor.

75     private _csi_regex:RegExp;
               ~~~~~~~~~~


Error: node_modules/ansi_up/ansi_up.ts:77:13 - error TS2564: Property '_osc_st' has no initializer and is not definitely assigned in the constructor.

77     private _osc_st:RegExp;
               ~~~~~~~


Error: node_modules/ansi_up/ansi_up.ts:78:13 - error TS2564: Property '_osc_regex' has no initializer and is not definitely assigned in the constructor.

78     private _osc_regex:RegExp;
               ~~~~~~~~~~


Error: node_modules/ansi_up/ansi_up.ts:85:13 - error TS2564: Property '_boldStyle' has no initializer and is not definitely assigned in the constructor.

85     private _boldStyle:string;
               ~~~~~~~~~~


Error: node_modules/ansi_up/ansi_up.ts:86:13 - error TS2564: Property '_faintStyle' has no initializer and is not definitely assigned in the constructor.

86     private _faintStyle:string;
               ~~~~~~~~~~~


Error: node_modules/ansi_up/ansi_up.ts:87:13 - error TS2564: Property '_italicStyle' has no initializer and is not definitely assigned in the constructor.

87     private _italicStyle:string;
               ~~~~~~~~~~~~


Error: node_modules/ansi_up/ansi_up.ts:88:13 - error TS2564: Property '_underlineStyle' has no initializer and is not definitely assigned in the constructor.

88     private _underlineStyle:string;
               ~~~~~~~~~~~~~~~


Error: node_modules/ansi_up/ansi_up.ts:101:9 - error TS2322: Type 'null' is not assignable to type 'AU_Color'.

101         this.fg = this.bg = null;
            ~~~~~~~


Error: node_modules/ansi_up/ansi_up.ts:101:19 - error TS2322: Type 'null' is not assignable to type 'AU_Color'.

101         this.fg = this.bg = null;
                      ~~~~~~~


Error: node_modules/ansi_up/ansi_up.ts:216:39 - error TS2769: No overload matches this call.
  The last overload gave the following error.
    Argument of type '(str: string) => "&amp;" | "&lt;" | "&gt;" | "&quot;" | "&#x27;" | undefined' is not assignable to parameter of type '(substring: string, ...args: any[]) => string'.
      Type 'string | undefined' is not assignable to type 'string'.
        Type 'undefined' is not assignable to type 'string'.

216       return txt.replace(/[&<>"']/gm, (str) => {
                                          ~~~~~~~~~~

  node_modules/typescript/lib/lib.es5.d.ts:470:5
    470     replace(searchValue: string | RegExp, replacer: (substring: string, ...args: any[]) => string): string;
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The last overload is declared here.


Error: node_modules/ansi_up/ansi_up.ts:216:39 - error TS7030: Not all code paths return a value.

216       return txt.replace(/[&<>"']/gm, (str) => {
                                          ~~~~~~~~~~


Error: node_modules/ansi_up/ansi_up.ts:231:31 - error TS2366: Function lacks ending return statement and return type does not include 'undefined'.

231     private get_next_packet():TextPacket {
                                  ~~~~~~~~~~


Error: node_modules/ansi_up/ansi_up.ts:612:30 - error TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
  Type 'undefined' is not assignable to type 'string'.

612           let num = parseInt(sgr_cmd_str, 10);
                                 ~~~~~~~~~~~


Error: node_modules/ansi_up/ansi_up.ts:617:15 - error TS2322: Type 'null' is not assignable to type 'AU_Color'.

617               this.fg        = null;
                  ~~~~~~~


Error: node_modules/ansi_up/ansi_up.ts:618:15 - error TS2322: Type 'null' is not assignable to type 'AU_Color'.

618               this.bg        = null;
                  ~~~~~~~


Error: node_modules/ansi_up/ansi_up.ts:633:36 - error TS2322: Type 'null' is not assignable to type 'AU_Color'.

633           } else if (num === 39) { this.fg = null;
                                       ~~~~~~~


Error: node_modules/ansi_up/ansi_up.ts:634:36 - error TS2322: Type 'null' is not assignable to type 'AU_Color'.

634           } else if (num === 49) { this.bg = null;
                                       ~~~~~~~


Error: node_modules/ansi_up/ansi_up.ts:654:52 - error TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
  Type 'undefined' is not assignable to type 'string'.

654                       let palette_index = parseInt(sgr_cmds.shift(), 10);
                                                       ~~~~~~~~~~~~~~~~


Error: node_modules/ansi_up/ansi_up.ts:665:40 - error TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
  Type 'undefined' is not assignable to type 'string'.

665                       let r = parseInt(sgr_cmds.shift(), 10);
                                           ~~~~~~~~~~~~~~~~


Error: node_modules/ansi_up/ansi_up.ts:666:40 - error TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
  Type 'undefined' is not assignable to type 'string'.

666                       let g = parseInt(sgr_cmds.shift(), 10);
                                           ~~~~~~~~~~~~~~~~


Error: node_modules/ansi_up/ansi_up.ts:667:40 - error TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
  Type 'undefined' is not assignable to type 'string'.

667                       let b = parseInt(sgr_cmds.shift(), 10);
                                           ~~~~~~~~~~~~~~~~


Error: node_modules/ansi_up/ansi_up.ts:749:15 - error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'.
  No index signature with a parameter of type 'string' was found on type '{}'.

749         if (! this._url_allowlist[parts[0]])
                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Error: node_modules/ansi_up/ansi_up.ts:762:14 - error TS7006: Parameter 'tmplObj' implicitly has an 'any' type.

762 function rgx(tmplObj, ...subst) {
                 ~~~~~~~


Error: node_modules/ansi_up/ansi_up.ts:762:23 - error TS7019: Rest parameter 'subst' implicitly has an 'any[]' type.

762 function rgx(tmplObj, ...subst) {
                          ~~~~~~~~


Error: node_modules/ansi_up/ansi_up.ts:774:15 - error TS7006: Parameter 'tmplObj' implicitly has an 'any' type.

774 function rgxG(tmplObj, ...subst) {
                  ~~~~~~~


Error: node_modules/ansi_up/ansi_up.ts:774:24 - error TS7019: Rest parameter 'subst' implicitly has an 'any[]' type.

774 function rgxG(tmplObj, ...subst) {
                           ~~~~~~~~

@drudru
Copy link
Owner

drudru commented Aug 16, 2023

Sounds good.

If you can look into why those errors are happening, that would be helpful.
If you check out the repo, do the npm module installs, do a make and make test.
I am curious if it builds.

@silverwind
Copy link

silverwind commented Aug 18, 2023

The export is now named instead of default so instead of

import AnsiUp from 'ansi_up';

you need to use

import {AnsiUp} from 'ansi_up';

I have just migrated a project to v6 with this change. In case it still does not work, it might be because your toolchain is not ESM-compatible, you can try these suggestions.

@ollischlueter
Copy link

Hi @drudru,
I am facing the same import problems as described by @Farbfetzen. I guess I have identified the main cause of this issue.

The issue appears to be in the package.json. The "types" field is still pointing to the /dist/-folder:

"types": "./dist/ansi_up.d.ts"

Because of this, when trying to import ansi_up using:

import { AnsiUp } from 'ansi_up';

The following error is produced:

Could not find a declaration file for module 'ansi_up'. '<...>/node_modules/ansi_up/ansi_up.js' implicitly has an 'any' type.
   Try `npm i --save-dev @types/ansi_up` if it exists or add a new declaration (.d.ts) file containing `declare module 'ansi_up';`ts(7016)

@Farbfetzen: Two temporary Workarounds that worked for me:

  1. Using TypeScript Ignore:
    You could temporarily bypass the TypeScript compiler error by adding a @ts-ignore comment above the import:
// @ts-ignore
import { AnsiUp } from 'ansi_up';
  1. Manual Update in package.json:
    Or you could manually modify the "types" path in the package.json of the ansi_up module in your node_modules folder in the meantime:
"types": "./ansi_up.d.ts"

@drudru
Copy link
Owner

drudru commented Aug 25, 2023

@ollischlueter - ah - will update this in 6.0.2. Thank you!

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

No branches or pull requests

4 participants