first commit
This commit is contained in:
18
utils/switchCase.ts
Normal file
18
utils/switchCase.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
export function switchCase<T extends Record<string, any>, K extends keyof T>(
|
||||
data: T | null | undefined,
|
||||
select?: K,
|
||||
): T[K] | string {
|
||||
if (!data) {
|
||||
return 'Tidak ada data'
|
||||
}
|
||||
|
||||
const key: K | 'default' = select !== undefined ? select : ('default' as K) // Type assertion needed here
|
||||
|
||||
if (key in data) {
|
||||
return data[key]
|
||||
} else if ('default' in data) {
|
||||
return data['default']
|
||||
} else {
|
||||
return 'Data tidak ditemukan'
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user