Skip to content

Commit

Permalink
Error: typescript doesn't type infer switch statement
Browse files Browse the repository at this point in the history
  • Loading branch information
letwebdev committed Sep 16, 2023
1 parent c936b34 commit 5def84a
Showing 1 changed file with 37 additions and 30 deletions.
67 changes: 37 additions & 30 deletions src/views/HackerNewsView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface List {
readonly description: string
}
type Lists = List[]
type LiveData = number | number[] | object
interface Item {
readonly by: string
readonly id: number
Expand Down Expand Up @@ -71,40 +72,46 @@ function fetchItems(listName: string = "topstories") {
throw new Error(`HTTP error: ${response.status}`)
}
})
.then((liveData: number | number[] | object) => {
.then((liveData: LiveData) => {
console.log("Type of liveData is " + typeof liveData)
if (typeof liveData === "number") {
console.log("Live data is currently largest item id: " + liveData)
const maxItemId: number = liveData
const randomItemId = generateRandomInteger(maxItemId)
fetchItem(randomItemId)
} else if (Array.isArray(liveData)) {
console.log("Live data is an array ")
let itemIds: number[]
if (settings.fetchingRandomly.enabled) {
const idsGenerantedRandomly: number[] = []
// TODO array.length may be less than maximumDisplayedItemsPerPage
for (let i = 0; i < settings.maximumDisplayedItemsPerPage.value; i++) {
const randomArrayIndex = generateRandomInteger(liveData.length - 1)
const idToPush = liveData[randomArrayIndex]
// Prevent duplicate id
if (idToPush === idsGenerantedRandomly[-1]) {
i--
continue
} else {
idsGenerantedRandomly.push(idToPush)
switch (true) {
case typeof liveData === "number": {
console.log("Live data is currently largest item id: " + liveData)
const maxItemId: number = liveData
const randomItemId = generateRandomInteger(maxItemId)
fetchItem(randomItemId)
break
}
case Array.isArray(liveData): {
console.log("Live data is an array ")
let itemIds: number[]
if (settings.fetchingRandomly.enabled) {
const idsGenerantedRandomly: number[] = []
// TODO array.length may be less than maximumDisplayedItemsPerPage
for (let i = 0; i < settings.maximumDisplayedItemsPerPage.value; i++) {
const randomArrayIndex = generateRandomInteger(liveData.length - 1)
const idToPush = liveData[randomArrayIndex]
// Prevent duplicate id
if (idToPush === idsGenerantedRandomly[-1]) {
i--
continue
} else {
idsGenerantedRandomly.push(idToPush)
}
}
itemIds = [...idsGenerantedRandomly]
} else {
itemIds = [...liveData]
}
itemIds = [...idsGenerantedRandomly]
} else {
itemIds = [...liveData]
itemIds.forEach((itemId) => {
fetchItem(itemId)
})
promptForFetching.value = ""
break
}
default: {
console.log("Unknwon live data type")
}
itemIds.forEach((itemId) => {
fetchItem(itemId)
})
promptForFetching.value = ""
} else {
console.log("Unknwon live data type")
}
})
.catch((error) => console.error(`Error fetching data: ${error.message}`))
Expand Down

0 comments on commit 5def84a

Please sign in to comment.