Skip to content

String indexer accepted as var type but not as class member type #178

Closed
@omidkrad

Description

@omidkrad

Hi,

I think this is a bug. Please see comments for details:

interface ObjectHash {
    [key: string]: any;
}

// OK   
var defaults: ObjectHash = {
    title: "untitled",
};

// This is error, expected
defaults = ["a"];

declare class Model {
    defaults: ObjectHash
}

// Index signatures of types '{ title: string }' and 'ObjectHash' are incompatibe.
// Why?! Unexpected error.
class MyModel extends Model {
    defaults = {
        title: "untitled"
    };
}

// It works when assigned in the constructor
class MyModel1 extends Model {
    constructor() {
        // OK
        this.defaults = {
            title: "untitled"
        }       
        super();
    }
}

// Trying different way
declare class Model2 {
    // 'defaults' is method
    defaults(): ObjectHash
}

// Same here. Why is this an error? Is this a bug?
class MyModel2 extends Model2 {
    defaults() {
        return {
            title: "untitled"
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    By DesignDeprecated - use "Working as Intended" or "Design Limitation" instead

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions