title: Sharing TypeScript type declarations via npm package
body: <p>I'm in need of sharing some TypeScript types between my React client and my Express REST API in order to keep the code clean and DRY.
  Since this is a private proejct I wouldn't share these types through the @types repository, so I've followed the guide on the TypeScript website and this is the result...</p>
  <p><strong>Everything is working just fine in the React client</strong>: I've installed the types as a dev dependency and used them flawlessly.</p>
  <p>In the Express API I get this error and I presume it has something to do with how I structured my package.</p>
  <p>What am I doing wrong?
  As ignorant as I am I'd suppose it's related with how the modules are loaded, but I can't figure out precisely what may be causing the error.</p>
  <pre><code>&gt; cross-env NODE_ENV=production node dist/index.js
  
  internal/modules/cjs/loader.js:834
    throw err;
    ^
  
  Error: Cannot find module '@revodigital/suiteods-types'
  </code></pre>
  <p>How I import the module inside the API code</p>
  <pre class="lang-js prettyprint-override"><code>import { AuthEntity, Roles } from '@revodigital/suiteods-types';
  
  @Model()
  export class AuthEntityModel implements AuthEntity {
    /* ... */
  
    role: Roles;
  
    /* ... */
  }
  
  </code></pre>
  <hr>
  Package tree
  <pre><code>suiteods-types
    |_index.d.ts
    |_package.json
    |_README.md
    |_tsconfig.json
  
  </code></pre>
  <p>index.d.ts</p>
  <pre class="lang-js prettyprint-override"><code>export = Ods;
  export as namespace Ods;
  
  declare namespace Ods {
    /* ... */
    interface AuthEntity extends DomainObject {
      email: string;
      password: string;
      role: Roles;
      instanceId: string;
    }
  
    enum Roles {
      BASE,
      STUDENT,
      BUSINESS,
      INSTRUCTOR,
      ADMIN
    }
    /* ... */
  }
  </code></pre>
  <p>package.json</p>
  <pre class="lang-json prettyprint-override"><code>{
    &quot;name&quot;: &quot;@revodigital/suiteods-types&quot;,
    &quot;version&quot;: &quot;0.1.1&quot;,
    &quot;description&quot;: &quot;Type declarations for suiteods project&quot;,
    &quot;types&quot;: &quot;index.d.ts&quot;,
    &quot;scripts&quot;: {
      &quot;test&quot;: &quot;echo \&quot;Error: no test specified\&quot; &amp;&amp; exit 1&quot;
    },
    &quot;author&quot;: &quot;Revo Digital&quot;,
    &quot;license&quot;: &quot;ISC&quot;,
    &quot;repository&quot;: {
      &quot;type&quot;: &quot;git&quot;,
      &quot;url&quot;: &quot;git+https://github.com/revodigital/suiteods-types.git&quot;
    },
    &quot;bugs&quot;: {
      &quot;url&quot;: &quot;https://github.com/revodigital/suiteods-types/issues&quot;
    },
    &quot;homepage&quot;: &quot;https://github.com/revodigital/suiteods-types#readme&quot;
  }
  </code></pre>
  <p>tsconfig.json</p>
  <pre class="lang-json prettyprint-override"><code>{
    &quot;compilerOptions&quot;: {
      &quot;module&quot;: &quot;commonjs&quot;,
      &quot;lib&quot;: [
        &quot;es6&quot;
      ],
      &quot;noImplicitAny&quot;: true,
      &quot;noImplicitThis&quot;: true,
      &quot;strictNullChecks&quot;: true,
      &quot;strictFunctionTypes&quot;: true,
      &quot;baseUrl&quot;: &quot;../&quot;,
      &quot;typeRoots&quot;: [
        &quot;../&quot;
      ],
      &quot;types&quot;: [],
      &quot;noEmit&quot;: true,
      &quot;forceConsistentCasingInFileNames&quot;: true
    },
    &quot;files&quot;: [
      &quot;index.d.ts&quot;
    ]
  }
  </code></pre>
  <hr>
  <h3> Update </h3>
  Confused on how to get rid of the namespace, and still getting the same error on the module, now installed as `dependency` and not as `devDependency`.
  The file structure is the same as above.
  Thanks in advance for the help.
  <p><em>updated and complete</em> index.d.ts</p>
  <pre class="lang-js prettyprint-override"><code>export = Ods;
  export as namespace Ods;
  
  declare namespace Ods {
  
    type IdType = 'Carta Identità' | 'Passaporto' | 'Patente'
    
    type ODSModule = 'SCUOLA' | 'OPERATORI' | 'DRONI' | 'ODS_ROOT'
  
    type Role = 'BASE' | 'STUDENTE' | 'ISTRUTTORE' | 'AMMINISTRATORE' | 'UTENTE_AZIENDALE'
    
    type UserScope = 'INTERNAL' | 'WHOLE'
  
  
    interface Address {
      street: string;
      city: string;
      province: string;
      CAP: string;
    }
  
    interface Credentials {
      email: string;
      password: string;
    }
  
    interface LoggedEntity {
      authEntity: AuthEntity;
      baseUser: BaseUser;
    }
  
    interface ModulesInstancesMap {
      SCUOLA: string;
      OPERATORI: string;
      DRONI: string;
      ODS_ROOT: string;
    }
  
    interface MultiTenantController {
  
    }
  
    interface Tenant {
      _id: string;
      role: Role | ODSModule;
    }
  
    interface TenantInfo {
      tenant: Tenant;
      relativeGodRole: Role;
    }
  
    interface AuthEntity extends DomainObject {
      email: string;
      password: string;
      role: Role;
      instanceId: string;
    }
  
    interface BaseUser extends DomainObject {
      firstName: string;
      lastName: string;
      phone: string;
      address: Address;
      scope: UserScope;
    }
  
    interface BelongsToModule {
      module: ODSModule;
    }
  
    interface Business extends DomainObject {
      businessName: string;
      pIva: string;
      tel: string;
      pec: string;
      recipientCode: string;
      address: Address;
    }
  
    interface DomainObject {
      _id: string;
    }
  
    interface HasTenant {
      tenantInfo: TenantInfo;
    }
  
    interface Instructor extends BaseUser {
      licenseCode: string;
    }
  
    interface InternalWholeSuiteUser extends BaseUser {
      modulesInstancesMap: ModulesInstancesMap;
    }
  
    interface InternalModuleUser extends BaseUser, BelongsToModule {
      moduleInstanceId: string;
    }
  
    interface School extends Business, HasTenant {
      cApr: number;
    }
  
  
    interface Student extends BaseUser {
      stateIssuedIdNumber: string;
      stateIssuedIsType: IdType;
      job: string;
      businessId?: string;
    }
  }
  
  </code></pre>