【发布时间】:2021-07-15 08:46:23
【问题描述】:
我有一个与 javascript 反应的旧应用程序,但我启动了一个新应用程序,以慢慢将 .JS 代码迁移到 Typescript。
当它的 .js 构建成功时,我要迁移的第一个文件是一个配置文件。
重命名为 .TS 时出现此错误
/Users/xx/yy/lulo/src/adalConfig.ts
(13,54): Argument of type '{ tenant: string; clientId: string; endpoints: { api: string; }; 'apiUrl': string; cacheLocation: string; }' is not assignable to parameter of type 'AdalConfig'.
Types of property 'cacheLocation' are incompatible.
Type 'string' is not assignable to type '"localStorage" | "sessionStorage" | undefined'
文件是这样的:
import { AuthenticationContext, adalFetch, withAdalLogin } from 'react-adal';
export const adalConfig = {
tenant: 'xxxx-c220-48a2-a73f-1177fa2c098e',
clientId: 'xxxxx-bd54-456d-8aa7-f8cab3147fd2',
endpoints: {
api:'xxxxx-abaa-4519-82cf-e9d022b87536'
},
'apiUrl': 'https://xxxxx-app.azurewebsites.net/api',
cacheLocation: 'localStorage'
};
export const authContext = new AuthenticationContext(adalConfig);
export const adalApiFetch = (fetch, url, options) =>
adalFetch(authContext, adalConfig.endpoints.api, fetch, adalConfig.apiUrl+url, options);
export const withAdalLoginApi = withAdalLogin(authContext, adalConfig.endpoints.api);
【问题讨论】:
-
试试
cacheLocation: 'localStorage' as 'localStorage' -
哈哈,这行得通,为什么?将字符串转换为字符串?
-
请作为答案发帖
-
与其丑陋的
'localStorage' as 'localStorage'不如正确输入adalConfig:在声明时指定它的类型。 -
export const adalConfig: ...typenamehere.... = {
标签: javascript reactjs typescript