Skip to content

Commit 160a8cd

Browse files
committed
refactor: update TypeScript SDK path and improve type inference in combine functions
1 parent 08bea23 commit 160a8cd

6 files changed

Lines changed: 26 additions & 14 deletions

File tree

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"typescript.tsdk": "node_modules/typescript/lib",
2+
"js/ts.tsdk.path": "node_modules/typescript/lib",
33
"cSpell.enabled": true,
44
"eslint.enable": true,
55
"prettier.enable": true,

src/combine.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ export interface Combine {
1111
* @param deps An array of {@link ReadableLike}s to combine.
1212
* @returns A {@link Readable} with the combined values.
1313
*/
14-
<TDeps extends readonly ReadableLike[] = ReadableLike[]>(deps: TDeps): OwnedReadable<MapReadablesToValues<TDeps>>;
14+
<TDeps extends readonly ReadableLike[] = ReadableLike[]>(
15+
deps: [...TDeps],
16+
): OwnedReadable<MapReadablesToValues<TDeps>>;
1517
/**
1618
* Combines an array of {@link ReadableLike}s into a single {@link Readable} with transformed value.
1719
* @param deps - An array of {@link ReadableLike}s to combine.
@@ -20,7 +22,7 @@ export interface Combine {
2022
* @returns A {@link Readable} with the transformed values.
2123
*/
2224
<TDeps extends readonly ReadableLike[] = ReadableLike[], TValue = any>(
23-
deps: TDeps,
25+
deps: [...TDeps],
2426
transform: (...deps: MapReadablesToValues<TDeps>) => TValue,
2527
config?: Config<TValue>,
2628
): OwnedReadable<TValue>;
@@ -47,10 +49,10 @@ export interface Combine {
4749
* const v2$ = writable(0);
4850
*
4951
* const combined$ = combine([v1$, v2$], (v1, v2) => v1 + v2);
50-
* ```
52+
* ````
5153
*/
5254
export const combine: Combine = <TDeps extends readonly ReadableLike[], TValue = any>(
53-
deps: TDeps,
55+
deps: [...TDeps],
5456
transform?: (...deps: MapReadablesToValues<TDeps>) => TValue,
5557
config?: Config<TValue>,
5658
) =>

src/react/useCombine.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export interface UseCombine {
6060
* ```
6161
*/
6262
export const useCombine: UseCombine = <TDeps extends readonly ReadableLike[], TValue>(
63-
deps: TDeps,
63+
deps: [...TDeps],
6464
transform?: (...deps: MapReadablesToValues<TDeps>) => TValue,
6565
config?: Config<TValue>,
6666
): OwnedReadable<TValue> => {

src/readable.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export interface CreateReadable {
3535
*
3636
* @returns A tuple with the Readonly and a function to set the value.
3737
*/
38-
<TValue = any>(): [OwnedReadable<NoInfer<TValue> | undefined>, SetValue<NoInfer<TValue> | undefined>];
38+
<TValue = any>(): [OwnedReadable<TValue | undefined>, SetValue<TValue | undefined>];
3939
/**
4040
* Creates a Readonly with the given value.
4141
*
@@ -51,7 +51,7 @@ export interface CreateReadable {
5151
* @param config Optional custom config.
5252
* @returns A tuple with the Readonly and a function to set the value.
5353
*/
54-
<TValue = any>(value: TValue, config?: Config<TValue>): [OwnedReadable<NoInfer<TValue>>, SetValue<NoInfer<TValue>>];
54+
<TValue = any>(value: TValue, config?: Config<TValue>): [OwnedReadable<TValue>, SetValue<TValue>];
5555
/**
5656
* Creates a Readonly with the given value.
5757
*
@@ -62,7 +62,7 @@ export interface CreateReadable {
6262
<TValue = any>(
6363
value?: TValue,
6464
config?: Config<TValue>,
65-
): [OwnedReadable<NoInfer<TValue | undefined>>, SetValue<NoInfer<TValue | undefined>>];
65+
): [OwnedReadable<TValue | undefined>, SetValue<TValue | undefined>];
6666
}
6767

6868
/** @internal */
@@ -354,7 +354,7 @@ export class ReadableImpl<TValue = any> implements BatchTask {
354354
export const readable: CreateReadable = <TValue = any>(
355355
value?: TValue,
356356
config?: Config<TValue | undefined>,
357-
): [OwnedReadable<NoInfer<TValue> | undefined>, SetValue<NoInfer<TValue> | undefined>] => {
357+
): [OwnedReadable<TValue | undefined>, SetValue<TValue | undefined>] => {
358358
let currentValue = value;
359359

360360
const get = () => currentValue;
@@ -428,4 +428,4 @@ export interface CreateWritable {
428428
export const writable: CreateWritable = <TValue = any>(
429429
value?: TValue,
430430
config?: Config<TValue>,
431-
): OwnedWritable<NoInfer<TValue | undefined>> => toWritable(...readable(value, config));
431+
): OwnedWritable<TValue | undefined> => toWritable(...readable(value, config));

test/combine.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,14 @@ describe("combine", () => {
2424
dep1$.value = 3;
2525
expect(combined$.value).toBe(5);
2626
});
27+
28+
it("should infer type correctly", () => {
29+
const dep1$ = writable(1);
30+
const dep2$ = writable("str");
31+
const passNumber = (n: number): number => n;
32+
const passString = (s: string): string => s;
33+
const combined$ = combine([dep1$, dep2$], (v1, v2) => [passNumber(v1), passString(v2)]);
34+
35+
expect(combined$.value).toEqual([dep1$.value, dep2$.value]);
36+
});
2737
});

tsconfig.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"compilerOptions": {
3+
"ignoreDeprecations": "6.0",
34
"baseUrl": ".",
45
"declaration": true,
56
"esModuleInterop": true,
@@ -11,7 +12,7 @@
1112
"noFallthroughCasesInSwitch": true,
1213
"noImplicitOverride": true,
1314
"noUnusedParameters": true,
14-
"outDir": "dist",
15+
"types": ["node"],
1516
"paths": {
1617
"@embra/reactivity": ["src"],
1718
"@embra/reactivity/debug": ["src/debug"]
@@ -21,6 +22,5 @@
2122
"strict": true,
2223
"stripInternal": true,
2324
"target": "ESNext"
24-
},
25-
"include": ["src/**/*"]
25+
}
2626
}

0 commit comments

Comments
 (0)