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

Type merging for imported types only works if the type is imported from "node_modules/@types/" dir #39691

Closed
ezsh opened this issue Jul 21, 2020 · 6 comments · Fixed by #51715
Assignees
Labels
Fix Available A PR has been opened for this issue Needs Investigation This issue needs a team member to investigate its status. Rescheduled This issue was previously scheduled to an earlier milestone

Comments

@ezsh
Copy link

ezsh commented Jul 21, 2020

Consider the following example.

In typings_module:
vars.d.ts:

interface UserProperties {
}

declare global {
    const variable: UserProperties;
}

index.d.ts:

export as namespace ModuleNamespace;
export * from "./vars"

Now, we want to specify properties for the UserProperties interface.
a_project_file.d.ts:

declare module "typings_module" {
	export interface UserProperties {
		test: number;
	}
}

export {}

The bug is the type merging works only if the "typings_module" files are inside "node_modules/@types/" and does not work if the same files are referenced from another location via the "typeRoots" TSC option in "tsconfig.json".

TypeScript Version: 3.9.6

Search Terms: "type merging", "declaration files"

@RyanCavanaugh RyanCavanaugh added the Needs More Info The issue still hasn't been fully clarified label Jul 21, 2020
@RyanCavanaugh
Copy link
Member

It's hard for us to guess about file layouts from prose; can you make a sample repo or zip file?

@ezsh
Copy link
Author

ezsh commented Jul 21, 2020

The layout is simple: a typings module with two files (vars.d.ts and index.d.ts, see their content above). The thing is if I symlink a dir with those files in node_modules/@types, type merging works, but when the typings are plugged in via the "typeRoots" project options, the merging breaks. Except for the merging, the typings work fine in both configurations.

@RyanCavanaugh
Copy link
Member

It's hard for us to guess about file layouts from prose; can you make a sample repo or zip file?

@ezsh
Copy link
Author

ezsh commented Jul 21, 2020

Here is the real example where I discovered the problem: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/46236/files#diff-411587a1c5c471aa01bb2b97932f74f1R62

I'll try to create an MWE.

@ezsh
Copy link
Author

ezsh commented Jul 21, 2020

typescript-issue-39691-mwe.zip

Here is the MWE, please. Check out the client/index.ts file. To make it work, I do ln -s ../../../types/typings-module in the client/node_modules/@types/ dir.

@RyanCavanaugh RyanCavanaugh added Needs Investigation This issue needs a team member to investigate its status. and removed Needs More Info The issue still hasn't been fully clarified labels Jul 21, 2020
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.1.0 milestone Jul 21, 2020
@RyanCavanaugh RyanCavanaugh added the Rescheduled This issue was previously scheduled to an earlier milestone label Dec 11, 2020
@sheetalkamat
Copy link
Member

The issue here is that when the typings-module is included through custom type root the user source file extending interface (extension.d.ts in the example) cannot resolve "typings-module" from that source file, so it does not merge those two things together.
The reason being module resolution does not take into account the custom type roots only node_modules/@types which is what results in this. That issue is tracked by #37958 and is breaking change and i have been investigating this but it cannot be taken for 4.2.
Keeping this alive but marking for 4.3

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 Needs Investigation This issue needs a team member to investigate its status. Rescheduled This issue was previously scheduled to an earlier milestone
Projects
None yet
4 participants