Answer the question
In order to leave comments, you need to log in
How to fix "No index signature..." error when accessing object properties?
Type declarations:
type AuthMessagesKeys = 'login' | 'logout' | 'session'
type AuthMessagesValues = 'text' | 'type'
type AuthMessagesParams = {
[P in AuthMessagesValues]: string
}
type AuthMessages = {
[P in AuthMessagesKeys] : AuthMessagesParams
}
export { AuthMessages }
import Vue, { PropOptions } from 'vue'
import { AuthMessages } from '@/types/Auth'
export default Vue.extend({
mounted() {
const queryObject = this.$route.query
const queryKey: string = queryObject.message
if (message) {
const messageType = this.messages[queryKey].type // здесь
const messageText = this.messages[queryKey].text // и здесь - ошибка (см. ниже)
...
}
},
data() {
return {
messages: {
login: {
text: 'You have to be logged in',
type: 'warning'
},
logout: {
text: 'You have successfully logged out',
type: 'success'
},
session: {
text: 'Your session has been expired',
type: 'error'
}
} as PropOptions<AuthMessages>
}
}
})
this.messages[queryKey]
An error occurs
in expressions :Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'PropOptions'.
No index signature with a parameter of type 'string' was found on type 'PropOptions'
Answer the question
In order to leave comments, you need to log in
const queryKey: AuthMessagesKeys = queryObject.message
function isAuthMessagesKey(str: string): str is AuthMessagesKeys {
return ['login', 'logout', 'session'].includes(str);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question