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

Spurious 'Index signature is missing in type' error when subclassing #1887

Closed
NoelAbrahams opened this issue Feb 2, 2015 · 6 comments
Closed
Labels
Duplicate An existing issue was already created Suggestion An idea for TypeScript

Comments

@NoelAbrahams
Copy link

Hi,

TS: 1.4

Not sure that I agree we should have an error here:

interface Indexer {
        [x: string]: number
}

class Base {

 foo() : Indexer {
    return undefined;
 }
}

/*Error: Class 'Foo' incorrectly extends base class 'Base'.
 Types of property 'foo' are incompatible.
 Type '() => { id: number; }' is not assignable to type '() => Indexer'.
 Type '{ id: number; }' is not assignable to type 'Indexer'.
 Index signature is missing in type '{ id: number; }'.
*/  
class Foo extends Base {

 foo() {
    return { id: 10 };
 }
}
@jbrantly
Copy link

jbrantly commented Feb 2, 2015

I ran into a variation of this with properties and static properties that is affecting me:

interface Indexer {
  [x: string]: number
}

class Base {
  static foo: Indexer;
  bar: Indexer;
}

class Foo extends Base {
  static foo = { id: 10 } // Class 'Foo' incorrectly extends base class 'Base'
  bar = { id: 10 } // Class 'Foo' incorrectly extends base class 'Base'
}

Foo.foo = { id: 10 } // no error (which seems correct)

@NoelAbrahams
Copy link
Author

Seems related. In my case the error goes away when foo is annotated:

 foo() : Indexer {
    return { id: 10 };
 }

One shouldn't have to do this.

@ahejlsberg
Copy link
Member

This looks to be a duplicate of #1373

@ahejlsberg ahejlsberg added Duplicate An existing issue was already created Suggestion An idea for TypeScript labels Feb 2, 2015
@RyanCavanaugh
Copy link
Member

See also http://stackoverflow.com/questions/22077023/why-cant-i-indirectly-return-an-object-literal-to-satisfy-an-index-signature-re

The last piece of the puzzle is that subclass methods are not contextually typed by their base class function signatures.

(aaand Anders referenced issue I was about to link once I found it)

@NoelAbrahams
Copy link
Author

The last piece of the puzzle is that subclass methods are not contextually typed by their base class function signatures.

Yes, I see that now. It's all the more confusing because the error message contains information about the mismatch between the base and subclasses. And one is left scratching their head thinking "if the compiler already knows about this, why then is it complaining?!"

I keep getting tripped up by the contextual typing of objects with index signatures. (I feel I've been referred to that SO post before!) The fact that it has taken a long post on StackOverflow kind of hints at the usability issues surrounding this.

Nevertheless it's a very informative response. Thanks!

@codermannn
Copy link

@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants