小编Tim*_*thy的帖子

如何正确处理多种类型的 TypeScript 值

我有以下 TS 接口

export interface Item {
  product: string | Product;
}
Run Code Online (Sandbox Code Playgroud)

当我想遍历项目数组时,我必须消除类型。换句话说

items = Items[];
items.forEach(item => {
  item.product._id
})
Run Code Online (Sandbox Code Playgroud)

不起作用,因为属性不适用于字符串。因此我必须预先检查类型,即:

items = Items[];
items.forEach(item => {
  if (typeof item.product === 'object') item.product._id
})
Run Code Online (Sandbox Code Playgroud)

我真的不喜欢它的样子。你如何处理这种情况?

typescript angular

5
推荐指数
1
解决办法
9317
查看次数

如何验证由 createCustomToken() 创建的自定义 Firebase 身份验证令牌

我通过 createCustomToken() 创建了自定义身份验证令牌,请参阅https://firebase.google.com/docs/auth/admin/create-custom-tokens

但是稍后当我尝试通过 verifyIdToken() 函数验证此令牌时,它会引发以下错误

Error: verifyIdToken() expects an ID token, but was given a custom token. See https://firebase.google.com/docs/auth/admin/verify-id-tokens for details on how to retrieve an ID token.
Run Code Online (Sandbox Code Playgroud)

这是合理的,因为没有这样的ID......但我需要 - 只是验证令牌,类似于jwt.verify()......

有没有人遇到过这个问题,找到了什么解决方案?是否可以通过 jsonwebtoken 库验证 Firebase 身份验证令牌?

PS 我将在 Google Cloud Function 端点中使用验证

解决方案:看起来我刚刚找到了一个解决方案https://firebase.google.com/docs/auth/admin/verify-id-tokens#verify_id_tokens_using_a_third-party_jwt_library只需要从https://www.googleapis.com 获取公钥/robot/v1/metadata/x509/securetoken@system.gserviceaccount.com并使用 jsonwebtoken 库对其进行验证...

firebase firebase-authentication google-cloud-functions

5
推荐指数
1
解决办法
1772
查看次数

通过映射将类型(接口)强制转换为可观察

我一直在用Angular学习TypeScript。目前,我停留在这一刻。以前,我使用订阅方法,并且一切都可以正常工作,但不是我决定使用异步管道重写代码,但这只是行不通。

  1. RestAPI拒绝特定格式的请求

export interface responseFormat {
  success: boolean;
  data: any;
}
Run Code Online (Sandbox Code Playgroud)

  1. 返回的数据可以是不同的类型。在这种情况下,数据返回餐厅对象...也可能是不同对象的数组...

export interface restaurant {
  _id: string;
  name: string;
  description: string;
  images: file[];
  cuisines: cuisine[];
  blocked: boolean;
  created: Date;
  business_hours: object;
}
Run Code Online (Sandbox Code Playgroud)

  1. 以前,我使用以下代码(它可以正常工作):

/* Provider */

getRestaurant(): Observable<responseFormat> {
  return this.http.get<responseFormat>(environment.api + "/restaurant");
}

/* Component */

private restaurant: restaurant;

getRestaurant() {
  this.restaurantProvider.getRestaurant().subscribe(res => {
    if (res.success) this.restaurant = res.data;
  });
}
Run Code Online (Sandbox Code Playgroud)

结果是分配了变量的类型。...但是我不想保留订阅记录,因此我想使用Angular异步管道来转换代码...这就是我所做的... 它不起作用

/* Provider */
getRestaurant(): Observable<responseFormat> {
  return this.http.get<responseFormat>(environment.api + "/restaurant");
}

/* Component …
Run Code Online (Sandbox Code Playgroud)

observable typescript angular

4
推荐指数
1
解决办法
1万
查看次数

如何将 NgControl 状态传递给 Angular 中的子组件,实现 ControlValueAccessor?

假如

{
  provide: NG_VALUE_ACCESSOR,
  useExisting: forwardRef(() => TestingComponent),
  multi: true
}
Run Code Online (Sandbox Code Playgroud)

注入 NgControl

constructor(@Self() @Optional() public control: NgControl) {
  this.control && (this.control.valueAccessor = this);
}
Run Code Online (Sandbox Code Playgroud)

然而这里还缺少什么?

在此处输入图片说明

尽管@Eliseo 的回答非常有说明性,但仍有一个补充……如果您想同时使用外部验证器和内部验证器,则必须相应地设置父 NgControl 验证器。此外,如果你想使用验证,你需要使用 ngDoCheck 生命周期钩子来处理 NgControl 触摸状态,因为我下面是一个最终的工作解决方案

@Component({
    selector: 'app-testing',
    templateUrl: 'testing.component.html'
})
export class TestingComponent implements ControlValueAccessor, DoCheck, AfterViewInit {
    @Input()
    public required: boolean;

    @ViewChild('input', { read: NgControl })
    private inputNgModel: NgModel;

    public value: number;
    public ngControlTouched: boolean;

    constructor(@Optional() @Self() public ngControl: NgControl) {
        if (this.ngControl != null) this.ngControl.valueAccessor = this; …
Run Code Online (Sandbox Code Playgroud)

angular

4
推荐指数
1
解决办法
3970
查看次数

如何为 Firebase 设置多个环境

对于我的项目,我使用登台和生产环境。angular.json 中的 fileReplacements 属性用于更改文件:

"fileReplacements": [
  {
    "replace": "src/environments/environment.ts",
    "with": "src/environments/environment.prod.ts"
  },
  {
    "replace": "src/app.html",
    "with": "src/app.prod.html"
  }
]
Run Code Online (Sandbox Code Playgroud)

使用 .firebaserc 文件在环境之间进行更改。

现在我需要为每个环境使用不同的 firebase 配置文件( firebase.json 和 firebase.prod.json )是否可以做到?我想使用预部署脚本,但不知道如何

firebase

3
推荐指数
1
解决办法
3096
查看次数

TypeScript 将对象键从 PascalCase 递归转换为驼峰格式

我需要创建一个函数来将对象的键从 PascalCase 修改为 camelCase 格式。IE

const input = {
    FirstName: 'John',
    LastName: 'Smith'
};

const expectedOutput = {
    firstName: 'John',
    lastName: 'Smith'
};

// function looks as follows
function pascalToCamelCase<T>(input: T): CamelCaseKeys<T> {
    if (typeof input !== 'object' || input === null) {
        return input;
    }

    // do conversion....
    return output;
}
Run Code Online (Sandbox Code Playgroud)

但我不知道如何通过 CamelCaseKeys 相应地修改密钥

typescript

2
推荐指数
1
解决办法
549
查看次数