yaquawa
yaquawa

Reputation: 7338

TypeScript declaration merging not works?

I want to add a new member to an existing interface. But typescript keeps throwing errors.

// foo.js
export interface IOption {
    xOffset: number
}
import {IOption} from 'foo';

// I want to add a `yOffset` to IOption, but this won't work.
// I got an error: `Import declaration conflicts with local declaration of 'IOptions'`
interface IOption {
    yOffset: number
}

What's the problem with my code?

Upvotes: 1

Views: 710

Answers (2)

Mario Vernari
Mario Vernari

Reputation: 7304

You cannot extend an existing interface, but you can create another one with the same name, by using a trick:

import * as foo from 'foo';

interface IOption extends foo.IOptions {
    yOffset: number
}

Upvotes: -1

basarat
basarat

Reputation: 276209

You need to declare the interface for the same module.

Fix

import {IOption} from 'foo';

declare module 'foo' {

    interface IOption {
        yOffset: number
    }

}

Upvotes: 3

Related Questions