-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(signals): make patchState work with TS 5.4 (#4294)
- Loading branch information
1 parent
9c3f845
commit 6b440ee
Showing
2 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { expecter } from 'ts-snippet'; | ||
import { compilerOptions } from './helpers'; | ||
|
||
describe('patchState', () => { | ||
const expectSnippet = expecter( | ||
(code) => ` | ||
import { | ||
PartialStateUpdater, | ||
patchState, | ||
signalState, | ||
} from '@ngrx/signals'; | ||
const state = signalState({ count: 1, foo: 'bar' }); | ||
function increment(): PartialStateUpdater<{ count: number }> { | ||
return ({ count }) => ({ count: count + 1 }); | ||
} | ||
function addNumber(num: number): PartialStateUpdater<{ | ||
numbers: number[]; | ||
}> { | ||
return ({ numbers }) => ({ numbers: [...numbers, num] }); | ||
} | ||
${code} | ||
`, | ||
compilerOptions() | ||
); | ||
|
||
it('infers the state type from StateSignal', () => { | ||
expectSnippet('patchState(state, increment())').toSucceed(); | ||
expectSnippet("patchState(state, { foo: 'baz' })").toSucceed(); | ||
expectSnippet("patchState(state, { foo: 'baz' }, increment())").toSucceed(); | ||
expectSnippet("patchState(state, increment(), { foo: 'baz' })").toSucceed(); | ||
}); | ||
|
||
it('fails with wrong partial state object', () => { | ||
expectSnippet('patchState(state, { x: 1 })').toFail( | ||
/'x' does not exist in type 'Partial<{ count: number; foo: string; }>/ | ||
); | ||
expectSnippet("patchState(state, { foo: 'baz' }, { x: 1 })").toFail( | ||
/'x' does not exist in type 'Partial<{ count: number; foo: string; }>/ | ||
); | ||
expectSnippet('patchState(state, { x: 1 }, { count: 0 })').toFail( | ||
/'x' does not exist in type 'Partial<{ count: number; foo: string; }>/ | ||
); | ||
expectSnippet('patchState(state, increment(), { x: 1 })').toFail( | ||
/'x' does not exist in type 'Partial<{ count: number; foo: string; }>/ | ||
); | ||
expectSnippet('patchState(state, { x: 1 }, increment())').toFail( | ||
/'x' does not exist in type 'Partial<{ count: number; foo: string; }>/ | ||
); | ||
}); | ||
|
||
it('fails with wrong partial state updater', () => { | ||
expectSnippet('patchState(state, addNumber(10))').toFail( | ||
/Property 'numbers' is missing in type '{ count: number; foo: string; }'/ | ||
); | ||
expectSnippet('patchState(state, { count: 10 }, addNumber(10))').toFail( | ||
/Property 'numbers' is missing in type '{ count: number; foo: string; }'/ | ||
); | ||
expectSnippet('patchState(state, addNumber(10), { count: 10 })').toFail( | ||
/Property 'numbers' is missing in type '{ count: number; foo: string; }'/ | ||
); | ||
expectSnippet('patchState(state, increment(), addNumber(10))').toFail( | ||
/Property 'numbers' is missing in type '{ count: number; foo: string; }'/ | ||
); | ||
expectSnippet('patchState(state, addNumber(10), increment())').toFail( | ||
/Property 'numbers' is missing in type '{ count: number; foo: string; }'/ | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters