Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Broken TypeScript typings. #19

Closed
wujekbogdan opened this issue Nov 6, 2018 · 7 comments
Closed

Broken TypeScript typings. #19

wujekbogdan opened this issue Nov 6, 2018 · 7 comments

Comments

@wujekbogdan
Copy link

wujekbogdan commented Nov 6, 2018

When I import trucate-html using:

import * as truncate from "truncate-html";

Then I'm not able to call truncate("<p>my-html</p>") because TypeScript compiler complains that:

Cannot invoke an expression whose type lacks a call signature.

I'm also not able to call truncate.setup().

When I import trucate-html using:

import truncate from "truncate-html";

Then TypeScript compiler doesn't complain, but there are runtime errors after compilation because truncate is undefined when imported this way.

@oe
Copy link
Owner

oe commented Nov 7, 2018

This lib should be import via the second way. I run test this lib from scratch, just found that, you may need to update your tsconfig.json by adding "esModuleInterop": true to the compilerOptions.

check Typescript 2.7(just search esModuleInterop) for more details.

Here is the complete tsconfig.json I used to test for you to refer.

{
  "compilerOptions": {
    "sourceMap": true,
    "strict": true,
    "noImplicitReturns": true,
    "target": "es6",
    "module": "commonjs",
    "moduleResolution": "node",
    "lib": ["es7"],
    "experimentalDecorators": true,
    "emitDecoratorMetadata": false,
    "allowSyntheticDefaultImports": true,
    "suppressImplicitAnyIndexErrors": true,
    "strictFunctionTypes": false,
    "esModuleInterop": true,
    "allowJs": true,
    "baseUrl": "."
  },
  "exclude": ["node_modules"]
}

oe added a commit that referenced this issue Nov 7, 2018
2. added typescript usage notice in readme,  #19
@oe
Copy link
Owner

oe commented Nov 7, 2018

This lib exports both commonjs and es6 files, but commonjs file will be required by default , So you may need to add this option"esModuleInterop": true if your building tools don't support es6 module.

https://github.com/evecalm/truncate-html/blob/master/package.json#L5-L7

@wujekbogdan
Copy link
Author

Setting esModuleInterop to true solves the problem, but it also breaks other modules' imports. For example: days. I used to import it like import * as dayjs from "dayjs";, now I have to import it like import dayjs from "dayjs";. It happens because when esModuleInterop is set to true then it enables allowSyntheticDefaultImports under the hood.

From docs:

Emit __importStar and __importDefault helpers for runtime babel ecosystem compatibility and enable --allowSyntheticDefaultImports for typesystem compatibility.

@oe
Copy link
Owner

oe commented Nov 7, 2018

I'm a little confused, if set allowSyntheticDefaultImports to true, you will be able to keep using this style import * as dayjs from "dayjs"

check the code of this lib:

https://github.com/evecalm/truncate-html/blob/master/src/truncate.ts#L1

https://github.com/evecalm/truncate-html/blob/master/tsconfig.json#L2-L19

@oe
Copy link
Owner

oe commented Nov 8, 2018

close it, I assume you solved this problem.

Feel free to reopen it if you have any further questions.

@oe oe closed this as completed Nov 8, 2018
@wujekbogdan
Copy link
Author

@evecalm

I solved the problem differently. I use const truncate = require("truncate-html"); as a workaround to import the module because I don't want to change tsconfig settings. Such a change would affect all my existing codebase. The dayjs module is not the only one that would be affected. I gave it just an example.

@oe
Copy link
Owner

oe commented Nov 16, 2018

It should works in my way.

You may try it your self by following steps:

git clone https://github.com/evecalm/truncate-html.git
cd truncate-html
yarn add dayjs

then edit test/demo.ts by adding code:

import dayjs from 'dayjs'
console.log('test dayjs', dayjs.isDayjs())

run the code to test: yarn run dev

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants