打字稿-类型'ICreateJwksContextReturn'的参数不能分配给类型'APIGatewayProxyEvent'的参数

时间:2019-10-15 01:54:42

标签: typescript types interface

上下文

我对Typescript还是比较陌生,但遇到了一个我根本不理解的错误。我想知道是否有人可以解释为什么在我看来不应发生这种情况。

如果我让createJwksContext返回any,该错误就会消失。我目前正在这样做,不需要特殊输入。但是很高兴了解WTF正在进行中!

问题

完整错误:

Argument of type 'ICreateJwksContextReturn' is not assignable to parameter of type 'APIGatewayProxyEvent'.

Type 'ICreateJwksContextReturn' is missing the following properties from type 'APIGatewayProxyEvent': body, headers, multiValueHeaders, httpMethod, and 8 more.

const authCheck = await authInstance.checkScopesAndResolve(mockedEvent, ['test scope']);

相关代码

类型

export interface IAuth {
  checkScopesAndResolve: (arg0: APIGatewayProxyEvent, arg1: [string]) => Promise<boolean>;
}

export interface IModifiedObject {
  [name: string]: string
}

export interface IJwksClientMock {
  start: () => void;
  stop: () => Promise<void>;
  kid: () => string;
  token: (token: {}) => string;
}

export interface ICreateJwksContextReturn {
  accessToken: string;
  jwksMock: IJwksClientMock;
}

功能

function customMockedEvent(modificationObject: IModifiedObject): APIGatewayProxyEvent {
  return {
      ...
    }
}

function createJwksContext(userObject: { sub: string, scope: string} = { sub: 'test user', scope: 'test scope'}): ICreateJwksContextReturn {
    ....
   return { accessToken, jwksMock };
}

TEST-实际发生错误的地方。

it('Returned - No scopes supplied!', async (done) => {
        const {accessToken, jwksMock} = await createJwksContext({
          sub: 'test user',
          scope: '',
        })
        done();

        console.log('accessToken', accessToken);
        const accessToken1 = 'blad'

        const mockedEvent: APIGatewayProxyEvent  = customMockedEvent({
          authorization: `Bearer ${accessToken1}`
        }) as APIGatewayProxyEvent;
        console.log('mockedEvent', mockedEvent);


        const authInstance = new Auth();

        expect( async () => {
          await authInstance.checkScopesAndResolve(mockedEvent, ['test scope']);
          done();
        }).toThrow('No scopes supplied!');
        jwksMock.stop();
      })

0 个答案:

没有答案