如何在文字对象上正确使用接口

How to correctly use an interface on a literal object

我在打字稿上有点吃力。假设你有一个文字对象,它的值是用扩展运算符赋值的:

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: Property 'User' does not exist on type 'typeof import("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 的正确方法是什么?

问题在于隐式类型。

const state = reactive(defaultState())
这里的

State 没有显式定义,赋值为 reactive(defaultState)。这意味着它被类型化为 defaultState.

const defaultState = () => {
  return {
    profile: {
      id: '',
      displayName: '',
      givenName: '',
      surName: '',
    },
  }
}

defaultState 这里没有类型,因此具有返回对象的隐式类型。

所以当我们给state

赋值时
state.profile = { ...defaultState().profile, ...response.data }

其中 response.data 类型安全为 MicrosoftGraph.User,其中 displayName: string | null.

所以state.profile.displayName的类型是string但是,response.data.displayName的类型是string | null因此导致我们的TS错误。

解决方案

我们所要做的就是更好的类型安全 defaultState.

const defaultState = () => {
  return {
    profile: {
      id: '',
      displayName: '',
      givenName: '',
      surName: '',
    },
  } as { profile: MicrosoftGraph.User },
}