从 Angular 包格式库调用 Auth.signUp 会产生类型错误?

Calling Auth.signUp from an Angular Package Format Library produces type error?

大多数 aws-amplify 身份验证函数都可以从 Angular 库中调用。例如 Auth.signout() 工作正常。

但是我尝试将以下实现放入图书馆服务中:

  /**
   * @param firstName The first name
   * @param lastName The last name
   * @param email The email
   * @param password The password
   * @returns The signup result
   */
   export function signUp(
    firstName: string,
    lastName: string,
    email: string,
    password: string) {
    return Auth.signUp({
      username: email,
      password,
      attributes: {
        email,
        name: firstName,
        given_name: firstName,
        family_name: lastName
      }
    });
  }

但是,当像这样导入库中的任何函数时,例如:

import { Component } from '@angular/core';
import { getCurrentAuthenticatedUserAttributes} from 'amplifylibe'
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  title = 't1';
}

它产生以下错误:


Build at: 2022-02-04T20:58:53.577Z - Hash: bd84be34957baf56 - Time: 112ms

Error: node_modules/amplifylibe/lib/auth.functions.d.ts:1:23 - error TS2688: Cannot find type definition file for 'amazon-cognito-identity-js'.

1 /// <reference types="amazon-cognito-identity-js" />
                        ~~~~~~~~~~~~~~~~~~~~~~~~~~


Error: node_modules/amplifylibe/lib/auth.functions.d.ts:14:118 - error TS2307: Cannot find module 'amazon-cognito-identity-js' or its corresponding type declarations.

14 export declare function signUp(firstName: string, lastName: string, email: string, password: string): Promise<import("amazon-cognito-identity-js").ISignUpResult>;
                                                                                                                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

为了展示这一点,我发布了一个带有 NPM 函数的最小库:

https://www.npmjs.com/package/amplifylibe

对应的github仓库是: https://github.com/fireflysemantics/tsamplifyapidemotest

包含signup功能。我还创建了一个 Angular 13 应用程序,当 ng serve 是 运行:

时会产生错误
git clone git@github.com:fireflysemantics/tsamplifylibdemoapp.git
cd tsamplifylibdemoapp
npm i 
ng serve

关于如何解决这个问题有什么想法吗?

看起来修复非常简单。只需像这样声明一个 Promise<any> return 类型:

  /**
   * @returns The result promise
   */
  async getCurrentAuthenticatedUserAttributes(): Promise<any> {
    const { attributes } = await Auth.currentAuthenticatedUser();
    return attributes
  }

在没有声明 return 类型的情况下,Typescript 似乎会尝试从 amplify 中推断出正确的 return 类型,而这在应用程序编译时对库不可用。