check if in desktop in typescript

To check if your TypeScript application is running on a desktop platform, you can use the navigator object which provides information about the browser environment. Here's an example code snippet that you can use to check if your application is running on a desktop:

index.ts
const isDesktop = () => {
  const platform = navigator.platform.toLowerCase();
  return !platform.match(/(iphone|ipad|android|mobile)/i);
}

console.log(`Is Desktop: ${isDesktop()}`);
184 chars
7 lines

The navigator.platform property returns a string that represents the platform of the browser (i.e. the operating system). We convert the platform string to lowercase and then use a regular expression to check if it matches any mobile platform (i.e. iPhone, iPad, Android, or mobile).

If the platform doesn't match any mobile platform, it means that the application is running on a desktop. The isDesktop function returns true in this case, and false otherwise. The above code snippet logs the result of the isDesktop function to the console.

gistlibby LogSnag