简体   繁体   中英

Typescript: Named property type must be assignable to string indexer type

I'm just a poor programmer who inherited some TypeScript code. Sometimes you just want a clue what is going on without taking a few weeks off to become fluent in a new language. I have an interface:

interface IJQuery {
    addRecordInline(object: any);
}

I'm getting an error on addRecordInline:

"Named property type '(object any) => any' must be assignable to string indexer type 'HTMLElement': Type 'HTMLElement' has non-optional property 'accessKey' which is not present in type '(object any) => any'

Just any kind of clue as to what is going on would be appreciated. I've looked around the internet ... there are some posts about Indexers that seem the closest. But what is happening here? Just a pointer to some information. Thank you.

EDIT:

Examples of the interface being implemented:

interface IDocumentManager {
    UpdateForm: IJQuery;
    UpdateActionUrl: string;
    DocIdPrefix: string;
}

2nd EDIT:

Here is a class that implements the interface:

class MemberDocumentManager implements IDocumentManager {
    private ConfirmDeleteButton: IJQuery;
    // other declarations removed

    constructor() {
        this.ConfirmDeleteButton = $('#deleteConfirmButton');
    }
}

A JQuery object is being assigned to a member that implememnts the interface in question, is that the problem?

Based on the error, that is not the only definition of IJQuery . As far as I can tell this is specific to your project and is not part of the JQuery library.

Form the error, there is probably a definition of IJQuery that contains an indexer:

interface IJQuery {
    [name: string]: HTMLElement
}

Which means all properties defined on this type must be of type HTMLElement

interface IJQuery {
    addRecordInline(object: any) : void; // Not ok
    anElement: HTMLElement // OK
}

You can either relax the restriction by removing the indexer ( [name: string]: HTMLElement ) or define the method on another interface or change the indexer to return either an element or a function ( [name: string]: HTMLElement | Function )(although this will probably break your code in several places)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM