Skip to content

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

Description

@ezsh

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"

Activity

  1. RyanCavanaugh commented on Jul 21, 2020

    @RyanCavanaugh
    Member

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

  2. ezsh commented on Jul 21, 2020

    @ezsh
    Author

    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.

  3. RyanCavanaugh commented on Jul 21, 2020

    @RyanCavanaugh
    Member

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

  4. ezsh commented on Jul 21, 2020

    @ezsh
    Author

    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.

  5. ezsh commented on Jul 21, 2020

    @ezsh
    Author

    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.

  6. added
    Needs InvestigationThis issue needs a team member to investigate its status.
    and removed
    Needs More InfoThe issue still hasn't been fully clarified
    on Jul 21, 2020
  7. sheetalkamat commented on Jan 8, 2021

    @sheetalkamat
    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

  8. 5 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Fix AvailableA PR has been opened for this issueNeeds InvestigationThis issue needs a team member to investigate its status.RescheduledThis issue was previously scheduled to an earlier milestone

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions