Skip to content

Declaration merging does not work using .d.ts files #11170

@Emm

Description

@Emm

TypeScript Version: 2.0.3

Code

npm install --save moment moment-timezone @types/moment @types/moment-timezone
import * as moment from "moment";
import "moment-timezone";

console.log(moment().format());
console.log(moment().tz("America/New_York").format());

Expected behavior:

The code compiles without incident.

Actual behavior:

test.ts(5,22): error TS2339: Property 'tz' does not exist on type 'Moment'.

Discussion:

The file node_modules/moment/moment.d.ts (the underlying type definitions used by moment) declares:

declare function moment(): moment.Moment;
// [snip]
declare namespace moment {
   // [snip]
    interface Moment {
        // [snip]
        format(): string;
        // [snip]
    }
   // [snip]
}

export = moment

While node_modules/@types/moment-timezone/index.d.ts contains:

import * as moment from 'moment';

export as namespace MomentTimezone;
export = moment;

declare module "moment" {
    interface Moment {
        tz(): string;
        tz(timezone: string): Moment;
        zoneAbbr() :Moment;
        zoneName() :Moment;
    }
    // [snip]
}

This seems consistent with what the handbook says about declaration merging.

Metadata

Metadata

Assignees

Labels

@typesRelates to working with .d.ts files (declaration/definition files) from DefinitelyTypedBugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions