Skip to content

Unable to add property to interface after external import #1574

Closed
@eschwartz

Description

@eschwartz

Consider this code:

// myPlugin.ts
<reference path="typings/jquery/jquery.d.ts" />

// Add a plugin to the JQuery type
interface JQuery {
  myPlugin():JQuery;
}

var $myEl = $('foo').myPlugin();
var $bar = $('foo').find('bar');

This works fine. The problem comes when I mix this with an external import:

// myPlugin.ts
<reference path="typings/jquery/jquery.d.ts" />

import _ = require('underscore');

// Add a plugin to the JQuery type
interface JQuery {
  myPlugin():JQuery;
}

var $myEl = $('foo').myPlugin();
var $bar = $('foo').find('bar');
/// error TS2339: Property 'find' does not exist on type 'JQuery'.

This does not seem to be specific to JQuery/underscore, but happens whenever I add to an interface in the same file as an import.

I can work around this by moving the interface additions to another file:

// jquery.ext.d.ts
<reference path="typings/jquery/jquery.d.ts" />

interface JQuery {
  myPlugin();
}

// myPlugin.ts
<reference path="typings/jquery/jquery.d.ts" />
<reference path="jquery.ext.d.ts" />

import _ = require('underscore');

var $myEl = $('foo').myPlugin();
var $bar = $('foo').find('bar');

Is this a bug in the compiler, or expected behavior? If it is expected, is there any documentation I can look at?

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in codeRevisitAn issue worth coming back to

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions