我有一个生成的文件(我们称之为
generated.ts
),它给了我类似的东西:
...
export type GetCompanyForEmployeeQueryVariables = Exact<{
companyId: Scalars['String'];
}>;
export type GetCompanyForEmployeeQuery = { __typename?: 'Query', getCompany?: { __typename?: 'QueryGetCompanyError', source: Source, reasons: Array<GetCompanyRefusalReasons> } | { __typename?: 'QueryGetCompanySuccess', data: { __typename?: 'Company', id: string, name: string, mdmPlatforms: Array<DevicePlatform> } } | null };
export function useGetCompanyForEmployeeLazyQuery(baseOptions?: Apollo.LazyQueryHookOptions<GetCompanyForEmployeeQuery, GetCompanyForEmployeeQueryVariables>) {
const options = {...defaultOptions, ...baseOptions}
return Apollo.useLazyQuery<GetCompanyForEmployeeQuery, GetCompanyForEmployeeQueryVariables>(GetCompanyForEmployeeDocument, options);
}
...
我想做的是“动态”访问内部的某种类型,但我有这个错误:
Cannot use namespace 'GeneratedTypes' as a type.ts(2709)
。
import type * as Generated from './generated.ts'
type WorkingVariables = GeneratedTypes.GetCompanyForEmployeeQueryVariables;
// error: Cannot use namespace 'GeneratedTypes' as a type.ts(2709)
type NotWorkingVariables = GeneratedTypes['GetCompanyForEmployeeQueryVariables'];
// My usecase looks like (it just for the example)
type ComputedNameFromOtherStrings = Compute<QueryName>
type Variables = GeneratedTypes[ComputedNameFromOtherStrings];
尝试使用
typeof
:
type NotWorkingVariables = typeof GeneratedTypes['GetCompanyForEmployeeQueryVariables'];
或者:
import { type GetCompanyForEmployeeQueryVariables } from "yourmodule";