Try to type Item and getValue (WIP)

This commit is contained in:
Maximilian Harz 2025-01-31 10:38:54 +01:00
parent c9a66461e1
commit f5b7b9267f
3 changed files with 10 additions and 13 deletions

View File

@ -1,11 +1,6 @@
/* eslint-disable @typescript-eslint/restrict-plus-operands */ /* eslint-disable @typescript-eslint/restrict-plus-operands */
/* eslint-disable @typescript-eslint/no-unsafe-call */
/* eslint-disable @typescript-eslint/no-unsafe-return */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unnecessary-condition */ /* eslint-disable @typescript-eslint/no-unnecessary-condition */
/* eslint-disable @typescript-eslint/no-unsafe-argument */
/* eslint-disable @typescript-eslint/prefer-optional-chain */ /* eslint-disable @typescript-eslint/prefer-optional-chain */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
import { Children, isValidElement, useEffect, useState } from 'react' import { Children, isValidElement, useEffect, useState } from 'react'
import { Marker, Tooltip } from 'react-leaflet' import { Marker, Tooltip } from 'react-leaflet'
@ -319,7 +314,7 @@ export const Layer = ({
)} )}
<Tooltip offset={[0, -38]} direction='top'> <Tooltip offset={[0, -38]} direction='top'>
{item.name ? item.name : getValue(item, itemNameField)} {item.name ? item.name : `${getValue(item, itemNameField)}`}
</Tooltip> </Tooltip>
</Marker> </Marker>
) )

View File

@ -1,14 +1,17 @@
/* eslint-disable @typescript-eslint/no-unsafe-assignment */ import type { Item } from '#types/Item'
/* eslint-disable @typescript-eslint/no-unsafe-return */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */ function getNestedValue(obj: Object, path: string) {
export function getValue(obj, path) { re
}
export function getValue(obj: Item | undefined, path: string): Item | string | undefined {
if (!obj || typeof path !== 'string') return undefined if (!obj || typeof path !== 'string') return undefined
const pathArray = path.split('.') // Use a different variable for the split path const pathArray = path.split('.') // Use a different variable for the split path
for (let i = 0, len = pathArray.length; i < len; i++) { for (let i = 0, len = pathArray.length; i < len; i++) {
if (!obj) return undefined // Check if obj is falsy at each step if (!obj) return undefined // Check if obj is falsy at each step
// eslint-disable-next-line security/detect-object-injection // eslint-disable-next-line security/detect-object-injection
obj = obj[pathArray[i]] // Dive one level deeper obj = obj[pathArray[i]] as Item // Dive one level deeper
} }
return obj // Return the final value return obj // Return the final value
} }

3
src/types/Item.d.ts vendored
View File

@ -24,8 +24,7 @@ export interface Item {
slug?: string slug?: string
user_created?: UserItem user_created?: UserItem
image?: string image?: string
// eslint-disable-next-line @typescript-eslint/no-explicit-any group_type: string
[key: string]: any
/* constructor( /* constructor(
id: string, id: string,
name: string, name: string,