Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

typescript - Element implicitly has an 'any' type because type 'Window' has no index signature?

I'm trying to create a Factory class in Typescript, but running into the following error:

src/ts/classes/Factory.ts(8,10): error TS7017: Element implicitly has an 'any' type because type 'Window' has no index signature.

I tried searching for this error, but didn't see anything that quite matched what I'm wanting to do.

The following is my Factory class.

/**
 * @class Factory
 *
 * @description Returns object based on given class string
 */
class Factory {
    public class(className: string): any {
        return window[className];
    }
}

I would rather not just suppress implicit errors in the compiler.

Any suggestions or help would be much appreciated! If this is not the best way to go about doing this, I'm definitely open to changing it as well.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Another way to index on window, without having to add a declaration, is to cast it to type any:

return (window as any)[className];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...