I'm struggling to understand why my nullable type isn't being applied properly
Here's an illustration
interface Book {
name: string;
author: string;
reference: string;
category: string;
}
async function handleFetch<T>(endpoint: string, params: object): Promise<T | null> {
const querystring = Object.entries(params)
.map(([key, value]) => `${key}=${value}`)
.join('&');
try {
const response = await fetch(`/api/${endpoint}?${querystring}`);
const data = await response.json();
if (response.ok) {
return data[0] || null;
}
return null;
} catch {
throw new Error('Internal error');
}
}
export default {
getBook: (reference: string) => {
return handleFetch<Book>('books', { reference });
},
};
It seems like the function is supposed to return a nullable type https://i.sstatic.net/NjDYF.png
but upon exporting, it loses its nullable property https://i.sstatic.net/xeIny.png
I feel like I must be overlooking something, as this is new to me
Thank you