We are currently using the msal
library and are in the process of converting our javaScript code to TypeScript.
In the screenshot below, TypeScript correctly identifies the expected type for cacheLocation
, which can be either the string localStorage
, the string sessionStorage
, or undefined
. However, when attempting to assign a variable holding one of these values, it fails:
https://i.sstatic.net/DxJEt.png
The issue could be resolved by using the following syntax. The drawback in this scenario is that an incorrect value is not flagged as incorrect:
https://i.sstatic.net/V3UOj.png
After consulting the TS documentation on Type Assertions, it states that this behavior is intentional, with the developer being responsible for ensuring correct types. I had expected TypeScript to throw an error if an unknown value was used.
Is there a more efficient way to handle this? Or is this simply the standard practice? Apologies if this question sounds naive, as I am still learning TypeScript.
Code
import * as config from 'src/app-config.json'
import { Configuration, CacheLocation } from 'msal'
const test = 'wrongValue'
export function MSALConfigFactory(): Configuration {
return {
auth: {
clientId: config.auth.clientId,
authority: config.auth.authority,
validateAuthority: true,
redirectUri: config.auth.redirectUri,
postLogoutRedirectUri: config.auth.postLogoutRedirectUri,
navigateToLoginRequestUrl: true,
},
cache: {
cacheLocation: test as CacheLocation,
storeAuthStateInCookie: false,
},
}
}