We have a file containing an interface that is not recognized by tsc or by vscode.
src/sap-truck-roster/resolver/RosterResolver.ts:40:17 - error TS2304: Cannot find name 'Context'.
// src\shared\types\Context.d.ts
import { Roster } from '@sap-truck-roster/entity/Roster'
import { Account } from '@it-portal/entity/Account'
interface Context {
user: Account
dataSources: {
sapRosterApi: Roster
}
}
Apparently we need to use the option typeRoots in tsconfig as suggested here. But for one reason or another the interface Context is still not recognized. What are we missing here?
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@environment": ["src/environment"],
"@utils/*": ["src/utils/*"]
},
"typeRoots": ["./node_modules/@types", "./src/shared/types/**/*.d.ts"],
"rootDir": "./src",
"outDir": "./dist",
"target": "es6",
},
"exclude": ["node_modules"],
"include": ["./src/**/*.tsx", "./src/**/*.ts", "./src/**/*.d.ts"]
}
Using the interface:
import { Resolver, Arg, Query, Ctx, Field, ObjectType, createUnionType } from 'type-graphql'
import { Roster } from '@sap-truck-roster/entity/Roster'
import { plainToClass } from 'class-transformer'
@Resolver()
export class RosterResolver {
@Query(() => RosterQueryResultUnion)
async roster(
@Ctx() ctx: Context, // use Context without import
@Arg('date', () => String) date: string
): Promise<typeof RosterQueryResultUnion> {
const response = await ctx.dataSources.sapRosterApi.getRoster(date)
if (response.returnCode === 'OK') {
return plainToClass(RosterArray, {
data: response.data,
})
}
return plainToClass(ApiError, {
code: response.returnCode,
message: response.errorMessage,
})
}
}
exportandimportit works fine. But the goal of using.d.tsfiles is that this is no longer required and the interface is in the global scope.