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

Types provided don't seem to work #24

Closed
toddobryan-clever opened this issue Nov 26, 2018 · 6 comments
Closed

Types provided don't seem to work #24

toddobryan-clever opened this issue Nov 26, 2018 · 6 comments

Comments

@toddobryan-clever
Copy link

We can't get our previously working code to work with the new types provided.

import * as sizeof from 'object-sizeof'; results in error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'typeof 'object-sizeof'' has no compatible call signatures. at compile time, but...

import sizeof from 'object-sizeof'; results in object_sizeof_1.default is not a function at runtime.

@Fazendaaa
Copy link
Contributor

Fazendaaa commented Nov 27, 2018

@toddobryan-clever I've made these typings... Have you tried import { sizeof } 'object-sizeof'?

@toddobryan-clever
Copy link
Author

OK. I upgraded to the latest version of TypeScript (just to be sure), then I tried

import * as sizeof from 'object-sizeof';
import sizeof from 'object-sizeof';
import { sizeof } from 'object-sizeof';

None of those worked.

I also tried setting esModuleInterop and allowSyntheticDefaultImports to true in the tsconfig.json file and tried all three options again. Nothing worked. :-(

@toddobryan-clever
Copy link
Author

I've gotten it to work, but had to make a couple of changes. I'm not sure if there's an easier/better way to do this.

I changed the types declaration to

declare module 'object-sizeof' {
    /**
     * Calculates Bytes for the provided parameter
     *
     * @param object The given object/string/boolean/buffer
     * @returns The size
     */
    function sizeof<T>(object: T): number;

    export = sizeof;
}

Note export = instead of default.

But then my import has to be changed to the TS-only:

import sizeof = require('object-sizeof');

Maybe this is the only way to do it? I don't know enough about TypeScript to be sure.

@FanAs
Copy link

FanAs commented Jan 9, 2019

Doesn't work for me too.

@cryodream
Copy link

Does not work with the same ts(2349) error:

Cannot invoke an expression whose type lacks a call signature. Type 'typeof import("object-sizeof")' has no compatible call signatures.

miktam added a commit that referenced this issue May 8, 2019
@miktam miktam closed this as completed in 4db1f20 May 8, 2019
@seranus
Copy link

seranus commented May 10, 2023

Here's my hacky solution to make it work

index2.d.ts
function size<T>(object: T): number;

indexv2.js

module.exports = {
  size: (obj) => {
    let totalSize = 0

    if (obj !== null && typeof obj === 'object') {
      totalSize = objectSizeComplex(obj)
    } else {
      totalSize = objectSizeSimple(obj)
    }

    return totalSize
  }
}

code
import
import * as sizeof from 'object-sizeof';
call
sizeof.size<object>(obj);

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