Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Feature request: enforce generic types when passed to `ReactFireOptions`

未关闭
#383 0 条评论 3 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
45/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
停滞
技术栈
firebase, react, typescript
领域
frontend

调研方向

首先在 TypeScript 源代码中搜索 ReactFireOptions 接口以及 {[key: string]: unknown} 的出现位置。更新选项类型,使显式泛型约束 initialData 和 startWithValue,将适用的索引签名替换为 Record<string, unknown>,并验证不匹配的泛型值会被拒绝,同时未指定类型的选项仍可使用。

由索引模型根据 Issue 内容生成。

描述

v5
Enforce generic types when passed to ReactFireOptions, update various types

Hey guys, will try to keep this short. Right now ReactFireOptions is generic on T but the type of initialData, which should be only of_ type T is of type T | any.

export interface ReactFireOptions<T = unknown> {
  idField?: string;
  initialData?: T | any;
  /**
   * @deprecated use initialData instead
   */
  startWithValue?: T | any;
  suspense?: boolean;
}

The result of this is that the generic parameter is useless, as we don't get any type enforcement on initialData. For example, this is fine:

interface MyInterface {
    thing: string;
}

const foo: ReactFireOptions<MyInterface> = {
    initialData: {
        thing: 2 // no complaints
    }
}

My suggestion is to change ReactFireOptions to this:

export interface ReactFireOptions<T = any> {
  idField?: string;
  initialData?: T;
  /**
   * @deprecated use initialData instead
   */
  startWithValue?: T;
  suspense?: boolean;
}

This still allows users to pass whatever they want to initialData when not supplying a generic type, but will cause Typescript to complain when using a generic type and the value passed to initialData does not match the provided type.

interface MyInterface {
    thing: string;
}

const foo: ReactFireOptions<MyInterface> = {
    initialData: {
        thing: 2 // complains, "Type 'number' is not assignable to type 'string'."
    }
}

Additionally, there are a few places using { [key: string]: unknown }, which can be expressed more clearly with Record<string, unknown>.

I have a branch with the following changes but I can't push it as I don't have permissions. Not sure if I need to ask for them somewhere.

Cheers! Great project, and I'm finding it very useful.

主要语言
TypeScript
星标
3.6k
派生
403
平均合并
5 天 1 小时
30 天内合并 PR
10

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

FirebaseExtended/reactfire 的其他 Issue

查看 FirebaseExtended/reactfire 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。