FirebaseExtended / FirebaseExtended/reactfire

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

Đang mở
#383 0 bình luận 3 reaction 0 người được giao Xem trên GitHub
v5
Ngôn ngữ chính
TypeScript
Star
3.6k
Fork
403
Merge trung bình
14 giờ 53 phút
Pull request đã merge (30 ngày)
5

Mô tả

### 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`.

```ts
export interface ReactFireOptions {
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:

```ts
interface MyInterface {
thing: string;
}

const foo: ReactFireOptions = {
initialData: {
thing: 2 // no complaints
}
}
```

My suggestion is to change `ReactFireOptions` to this:

```ts
export interface ReactFireOptions {
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.

```ts
interface MyInterface {
thing: string;
}

const foo: ReactFireOptions = {
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`.

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.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.