【发布时间】:2020-11-30 19:49:24
【问题描述】:
我在打字稿上有点吃力。假设你有一个文字对象,它的值是用扩展运算符分配的:
const defaultState = () => {
return {
profile: {
id: '',
displayName: '',
givenName: '',
surName: '',
},
}
}
const state = reactive(defaultState())
const response = await getGraphProfile()
state.profile = { ...defaultState().profile, ...response.data }
在更新类型库@microsoft/microsoft-graph-types 后,抛出以下 TS 错误:
TS2322: Type '{ accountEnabled?: Maybe<boolean>; ageGroup?: string | null | undefined; assignedLicenses?: MicrosoftGraph.AssignedLicense[] | undefined; assignedPlans?: MicrosoftGraph.AssignedPlan[] | undefined; ... 102 more ...; surName: string; }' is not assignable to type '{ id: string; displayName: string; givenName: string; surName: string; jobTitle: string; mail: string; mobilePhone: string; officeLocation: string; businessPhones: string[]; preferredLanguage: string; userPrincipalName: string; }'.
Types of property 'displayName' are incompatible.
Type 'string | null' is not assignable to type 'string'.
Type 'null' is not assignable to type 'string'.
尝试像this answer 那样在文字对象上设置接口MicrosoftGraph.User 并没有解决它,因为我一定是语法有问题:
import * as MicrosoftGraph from '@microsoft/microsoft-graph-types'
const defaultState = () => {
return {
profile: MicrosoftGraph.User = {
id: '',
displayName: '',
givenName: '',
surName: '',
},
}
}
这会引发下面的 TS 错误,但User 接口肯定存在并且在函数getGraphProfile 中正确使用。
TS2339:“类型导入”类型上不存在属性“用户”(“T:/Test/Brecht/Node/prod/hip-frontend/node_modules/@microsoft/microsoft-graph-types/microsoft-graph”) '。
额外代码:
import config from 'src/app-config.json'
import axios, { AxiosRequestConfig } from 'axios'
import { getToken } from 'src/services/auth/authService'
import * as MicrosoftGraph from '@microsoft/microsoft-graph-types'
const callGraph = <T>(
url: string,
token: string,
axiosConfig?: AxiosRequestConfig
) => {
const params: AxiosRequestConfig = {
method: 'GET',
url: url,
headers: { Authorization: `Bearer ${token}` },
}
return axios.request<T>({ ...params, ...axiosConfig })
}
const getGraphDetails = async <T>(
uri: string,
scopes: string[],
axiosConfig?: AxiosRequestConfig
) => {
try {
const response = await getToken(scopes)
if (response && response.accessToken) {
return callGraph<T>(uri, response.accessToken, axiosConfig)
} else {
throw new Error('We could not get a token because of page redirect')
}
} catch (error) {
throw new Error(`We could not get a token: ${error}`)
}
}
export const getGraphProfile = async () => {
try {
return await getGraphDetails<MicrosoftGraph.User>(
config.resources.msGraphProfile.uri,
config.resources.msGraphProfile.scopes
)
} catch (error) {
throw new Error(`Failed retrieving the graph profile: ${error}`)
}
}
将displayName 属性保存为string | null 的正确方法是什么?
【问题讨论】:
-
如果在
defaultState中分配displayNamenull会发生什么? -
试过了,它会抛出
Type 'string | null' is not assignable to type 'null' -
我们聊天here
标签: javascript typescript vue.js vuejs2 vue-composition-api