Exported variable has or is using name from external module

In TypeScript, when you have an exported variable that uses a private type from an external module, you may run into issues because private types are not accessible outside of their module.

answers for exported variable that uses a private type from an external module:

  • Exported variable ‘yourVariable’ has or is using name ‘TypeName’ from external module “module/path” but cannot be named. ts(4023)
  • TS4023: Exported Variable has or is using name from external module but cannot be named
  • Exported variable X has or is using name Y from external module Z but cannot be named
  • Exported variable ‘projectConfig’ has or is using name ‘Config’ from external module site:stackoverflow.com
  • To fix exported variable using private type from external module, you can consider a few approaches:

    Fix steps

    just declare the type, for example:

    in external lib has code like below:

    interface privateProperty {
      name: string;
    }
    export const config = {
      propName: {} as privateProperty
    }

    in your code

    // this config['propName'] contains non-exported type/interface
    import { config } from 'external-lib';
    
    // declare new type from private property
    type Y = config['propName']; // or typeof config['propName']
    // declare new type that extends Y
    export interface X extends Y {}
    // apply interface X which contains Private Type Name
    export const yourVariable = {} as X;

    Conclusion

    done. now the problem fixed

    result

    before
    Exported variable has or is using name from external module | WMI - https://i.stack.imgur.com/6zwQS.png

    after
    Exported variable has or is using name from external module | WMI - https://i.stack.imgur.com/uVeAs.png

    Created at 2024-02-03 19:04:52
    Updated at 2024-02-03 19:07:22
    Author: Dimas Lanjaka
    Link: https://www.webmanajemen.com/2024/02/typescript-error-private-name.html
    Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
    avatar
    Dimas Lanjaka
    Website Management Indonesia is a blog about scripts, tips and tricks, games, software. Covering php, javascript, jquery, mysql, seo, e-commerce and others.
    Follow Me
    Recent Post
    myspace tracker