Currently, I am attempting to implement the following code snippet for browser detection. However, I am encountering an error in relation to browserData
, which states:
Type '{ browserName: () => string; browserVersion: string | null; operatingSystem: string | (() => string); } | { browserName: string; browserVersion: string; operatingSystem: string; }' is not assignable to type 'BrowserData'.
Type '{ browserName: () => string; browserVersion: string | null; operatingSystem: string | (() => string); }' is not assignable to type 'BrowserData'.
Types of property 'browserName' are incompatible.
Type '() => string' is not assignable to type 'string'.ts(2322)
function getBrowserInfo(): BrowserData {
const browserInfo = detectBrowser.detect();
const browserData: BrowserData =
browserInfo !== null
? {
browserName: browserInfo.name.toString,
browserVersion: browserInfo.version,
operatingSystem:
browserInfo.os !== null ? browserInfo.os.toString : '',
}
: { browserName: '', browserVersion: '', operatingSystem: '' };
return browserData;
}
declare type BrowserData = {
browserName: string;
browserVersion: string;
operatingSystem: string;
};
The issue appears to stem from the fact that the expected return value should be a string, rather than a function that produces a string. How can I go about rectifying this discrepancy?