TypeScript: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type
TypeScript Version: 3.0.1
Code
import Sequelize from 'sequelize';
import { DbInterface } from'typings/DbInterface';
import { UserFactory } from './User';
import { PostFactory } from './Post';
import { CommentFactory } from './Comment';
export const createModels = (sequelizeConfig: any): DbInterface => {
const { database, username, password, params } = sequelizeConfig;
const sequelize = new Sequelize(database, username, password, params);
const db: DbInterface = {
sequelize,
Sequelize,
Comment: CommentFactory(sequelize, Sequelize),
Post: PostFactory(sequelize, Sequelize),
User: UserFactory(sequelize, Sequelize)
};
Object.keys(db).forEach(modelName => {
if (db[modelName].associate) { //Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'DbInterface'. No index signature with a parameter of type 'string' was found on type 'DbInterface'.
db[modelName].associate(db); //Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'DbInterface'. No index signature with a parameter of type 'string' was found on type 'DbInterface'.
}
});
return db;
};
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 12
- Comments: 16 (1 by maintainers)
This is a question, not a bug, please use SO for questions. (see this it will help)
Bad - the reason for the error is the
objecttype is just an empty object by default. Therefore it isn’t possible to use astringtype to index{}.Better - the reason the error disappears is because now we are telling the compiler the
objargument will be a collection of string/value (string/any) pairs. However, we are using theanytype, so we can do better.Best -
Textends empty object.Uextends the keys ofT. ThereforeUwill always exist onT, therefore it can be used as a look up value.Just found this blog post, which was very helpful, and wanted to paste some code here that I think is slightly more straight forward than the code included here so far. If you have the following object:
You can declare types for the keys and values in that object like so:
@greenlaw110 Try to get the key value by using @alexandermckay’s method:
In addition to the answers, people could also try to use interfaces 😃 Here is an example, hope it helps:
I am a bit confused by this specific type checking. It seems to me that:
undefined.Going further with the example above:
I see 3 ways of bypassing this:
…What I would expect: As far as I know, accessing a key that does not exist in javascript will always return
undefined. So in my opinion, accessing such key should actually emit a type that is a union ofundefinedand all other values in the object. But certainly not ‘any’.This way I could do;
For those of you having this issue with typescript:
// error
Like @alexandermckay said, we need to specify that the current record’s keys are of type “string” //fix
So extend your interfaces with Record<string, any>, or generic types to get rid of any.
Hope this helps.
Hey, if I do this my object will have the following shape when I hove over it:
so auto-complete is no longer available, am I missing something?
Same question here, I have
Unfortunately, weakly typed solutions make autocompletion no longer work.