Reputation: 23
This is the full error I get when I hover on data[command] in limit-entries.ts
(parameter) data: dataProps Element implicitly has an 'any' type because expression of type '"hourly"' can't be used to index type 'dataProps'. Property 'hourly' does not exist on type 'dataProps'
darkSkyApiTypes.ts
interface dataProps {
data: {
latitude: number
longitude: number
timezone: string
currently?: {
time: number
summary: string
temperature: number
humidity: number
}
hourly?: {
summary: string
icon?: string
data: {
time: number
temperature: number
humidity: number
}[]
}
daily?: {
summary: string
icon?: string
data: {
time: number
temperatureMin: number
temperatureMax: number
humidity: number
}[]
}
}
}
export = dataProps
limit-entries.ts
import { hourlyLimit } from '../config'
import dataProps from '../API/darkSkyAPITypes'
const limitEntries = (data: dataProps, command: string) => {
let entries
switch (command) {
case 'currently':
entries = data[command]
break
case 'hourly':
entries = data[command].data.slice(0, hourlyLimit)
break
case 'daily':
entries = data[command].data
break
default:
entries = data[command]
}
return entries
}
module.exports = limitEntries
I get the error in title. Help!
Upvotes: 0
Views: 413
Reputation: 1074295
dataProps
defines an object type with a single top-level property called data
. Your data
variable is of type dataProps
, so you'd need data.data
to refer to that property. So where you have data[command]
, you should have data.data[command]
. (Or change the definition of dataProps
.)
Upvotes: 1