You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
interfaceUser{name: string;}document.addEventListener("DOMContentLoaded",async()=>{constresponse=awaitfetch('/api/users/current-user');constuser=(awaitresponse.json())asUser;window.user=user;// ~~~~ Property 'user' does not exist// on type 'Window & typeof globalThis'.});// ... elsewhere ...exportfunctiongreetUser(){alert(`Hello ${window.user.name}!`);// ~~~~ Property 'user' does not exist on type Window...}
declare global {interfaceWindow{/** The currently logged-in user */user: User|undefined;}}// ...exportfunctiongreetUser(){alert(`Hello ${window.user.name}!`);// ~~~~~~~~~~~ 'window.user' is possibly 'undefined'.}
typeMyWindow=(typeofwindow)&{/** The currently logged-in user */user: User|undefined;}document.addEventListener("DOMContentLoaded",async()=>{constresponse=awaitfetch('/api/users/current-user');constuser=(awaitresponse.json())asUser;(windowasMyWindow).user=user;// OK});// ...exportfunctiongreetUser(){alert(`Hello ${(windowasMyWindow).user.name}!`);// ~~~~~~~~~~~~~~~~~~~~~~~~~ Object is possibly 'undefined'.}