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

triple-slash reference added only if another triple-slash reference exists #57482

Closed
dragomirtitian opened this issue Feb 22, 2024 · 3 comments Β· Fixed by #57681
Closed

triple-slash reference added only if another triple-slash reference exists #57482

dragomirtitian opened this issue Feb 22, 2024 · 3 comments Β· Fixed by #57681
Labels
Fix Available A PR has been opened for this issue Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Milestone

Comments

@dragomirtitian
Copy link
Contributor

πŸ”Ž Search Terms

triple-slash reference added

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

Workbench

πŸ’» Code

// @declaration: true
// @filename: /a/node_modules/extra/index.d.ts
interface Error2 {
    stack2: string;
}

// @filename: /a/node_modules/something/index.d.ts
/// <reference types="extra" />
// ^ remove above and it will no directive will be added to app.d.ts
export function foo():Error2;

// @filename: /a/app.ts
// reference to extra will be added here if there exists another reference to extra
export function foo() {
    return null! as Error2;
}

πŸ™ Actual behavior

/// <reference types="extra" /> is added to app.d.ts only if another reference directive to extra already exists (in this example one from node_modules)

πŸ™‚ Expected behavior

/// <reference types="extra" /> is always added since extra is required for app.d.ts to be valid

Additional information about the issue

This was discovered while trying to understand reference directives in the context of isolated declarations.

@dragomirtitian
Copy link
Contributor Author

@andrewbranch do you have any insight on if this behavior is intentional?

@andrewbranch
Copy link
Member

I feel like I recently stumbled across a piece of code that doesn’t add type reference directives if the types are found in typeRoots, but I don’t remember exactly where that was, and it feels unjustifiable to me.

@RyanCavanaugh RyanCavanaugh added the Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases label Feb 22, 2024
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Feb 22, 2024
@dragomirtitian
Copy link
Contributor Author

dragomirtitian commented Feb 22, 2024

This seems to be intentional, but I still don't understand why:

// checker.ts
function getTypeReferenceDirectivesForEntityName(node: EntityNameOrEntityNameExpression): [specifier: string, mode: SourceFile["impliedNodeFormat"] | undefined][] | undefined {
    // program does not have any files with type reference directives - bail out
    if (!fileToDirective) {
        return undefined;
    }

Also fileToDirective is populated from the host (const resolvedTypeReferenceDirectives = host.getResolvedTypeReferenceDirectives();) and getResolvedTypeReferenceDirectives just returns the cache of reference directives that were found while parsing the program SourceFiles.

This code seems to be really old (2016 by Vladimir Matveev)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Fix Available A PR has been opened for this issue Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants