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

Namespace missing in generated .d.ts #59470

Closed
d-eder opened this issue Jul 30, 2024 · 5 comments Β· Fixed by #59493
Closed

Namespace missing in generated .d.ts #59470

d-eder opened this issue Jul 30, 2024 · 5 comments Β· Fixed by #59493
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@d-eder
Copy link

d-eder commented Jul 30, 2024

πŸ”Ž Search Terms

namespace missing, import alias namespace missing

πŸ•— Version & Regression Information

  • This changed between versions >5.2.2 and <=5.5.4

⏯ Playground Link

https://stackblitz.com/edit/stackblitz-starters-fxkjvu

πŸ’» Code

File translation.ts:

export interface Translation {
  translationKey: Translation.TranslationKeyEnum;
}

export namespace Translation {
  export type TranslationKeyEnum = 'translation1' | 'translation2';
  export const TranslationKeyEnum = {
    Translation1: 'translation1' as TranslationKeyEnum,
    Translation2: 'translation2' as TranslationKeyEnum,
  }
}

File my-lib.component.ts:

import { Component } from '@angular/core';
import {Translation} from "./translation";
import TranslationKeyEnum = Translation.TranslationKeyEnum;

@Component({
  selector: 'lib-my-lib',
  standalone: true,
  imports: [],
  template: `
    <p>
      {{ TranslationKeyEnum.Translation1 }}
    </p>
  `,
  styles: ``
})
export class MyLibComponent {
  TranslationKeyEnum = TranslationKeyEnum;
}

πŸ™ Actual behavior

For the given typescript file, a declaration file translation.d.ts is generated which looks like:

export interface Translation {
    translationKey: Translation.TranslationKeyEnum;
}
export declare namespace Translation {
    type TranslationKeyEnum = 'translation1' | 'translation2';
    const TranslationKeyEnum: {
        Translation1: TranslationKeyEnum // should be Translation.TranslationKeyEnum;
        Translation2: TranslationKeyEnum;
    };
}

And similar for the component file my-lib.component.d.ts:

import { Translation } from "./translation";
import * as i0 from "@angular/core";
export declare class MyLibComponent {
    TranslationKeyEnum: {
        Translation1: TranslationKeyEnum;
        Translation2: TranslationKeyEnum;
    };
    static Ι΅fac: i0.Ι΅Ι΅FactoryDeclaration<MyLibComponent, never>;
    static Ι΅cmp: i0.Ι΅Ι΅ComponentDeclaration<MyLibComponent, "lib-my-lib", never, {}, {}, never, never, true, never>;
}

This will then create an error when building the application:


X [ERROR] TS2304: Cannot find name 'TranslationKeyEnum'. [plugin angular-compiler]

    dist/my-lib/lib/my-lib.component.d.ts:5:22:
      5 β”‚         Translation1: TranslationKeyEnum;
        β•΅                       ~~~~~~~~~~~~~~~~~~


X [ERROR] TS2304: Cannot find name 'TranslationKeyEnum'. [plugin angular-compiler]

    dist/my-lib/lib/my-lib.component.d.ts:6:22:
      6 β”‚         Translation2: TranslationKeyEnum;
        β•΅                       ~~~~~~~~~~~~~~~~~~

πŸ™‚ Expected behavior

On previous versions the namespace was also included:

export interface Translation {
    translationKey: Translation.TranslationKeyEnum;
}
export declare namespace Translation {
    type TranslationKeyEnum = 'translation1' | 'translation2';
    const TranslationKeyEnum: {
        Translation1: Translation.TranslationKeyEnum;
        Translation2: Translation.TranslationKeyEnum;
    };
}

Additional information about the issue

  • The translation.ts file got generated by swagger codegen
  • I'm not totally sure this issue belongs to typescript or the angular compiler
  • We explicitly had to set the ts-config flag "skipLibCheck": false
@d-eder d-eder changed the title Namespace missing in .d.ts file when using import alias Namespace missing in .d.ts Jul 30, 2024
@d-eder d-eder changed the title Namespace missing in .d.ts Namespace missing in generated .d.ts Jul 30, 2024
@MartinJohns
Copy link
Contributor

No error on the TypeScript side: Playground link

@d-eder
Copy link
Author

d-eder commented Jul 30, 2024

No error on the TypeScript side: Playground link

I tested this on bug workbench again:

There is no error. However, the file test.d.ts looks wrong:
image

There is no import declaration for TranslationKeyEnum

I also tried this with typescript 5.2.2: bug-workbench-5.2.2
Here it is correct (with the namespace):
image

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Jul 31, 2024
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 5.7.0 milestone Jul 31, 2024
@RyanCavanaugh
Copy link
Member

Confirmed the bug workbench repro locally

@jakebailey
Copy link
Member

Bisects to #57887

@jakebailey
Copy link
Member

Copy-pastable compiler test:

// @strict: true
// @declaration: true

// @filename: translation.ts
export interface Translation {
  translationKey: Translation.TranslationKeyEnum;
}

export namespace Translation {
  export type TranslationKeyEnum = 'translation1' | 'translation2';
  export const TranslationKeyEnum = {
    Translation1: 'translation1' as TranslationKeyEnum,
    Translation2: 'translation2' as TranslationKeyEnum,
  }
}


// @filename: test.ts 
import { Translation } from "./translation";
import TranslationKeyEnum = Translation.TranslationKeyEnum;

export class Test {
  TranslationKeyEnum = TranslationKeyEnum;
  print(){
    console.log(TranslationKeyEnum.Translation1);
  }
}

// @filename: index.ts 
import { Test } from "./test";
new Test().print();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants