-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
types: Improve types for store, storeState, effects (#36)
* IfSubset fix * add TSliceName to store and others * embed types inside * fixes
- Loading branch information
Showing
33 changed files
with
1,987 additions
and
4,717 deletions.
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
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
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,75 @@ | ||
import { expect, test } from '@jest/globals'; | ||
import { createKey, Key, Slice } from '@nalanda/core'; | ||
import { | ||
InferSliceDepNames, | ||
InferSliceName, | ||
expectType, | ||
IfEquals, | ||
} from '../type-helpers'; | ||
|
||
const dep1Key = createKey('dep1Key', []); | ||
const dep1Slice = dep1Key.slice({ | ||
fields: {}, | ||
}); | ||
|
||
const dep2Key = createKey('dep2Key', []); | ||
const dep2Slice = dep2Key.slice({ | ||
fields: {}, | ||
}); | ||
|
||
const key = createKey('myKey', [dep1Slice, dep2Slice]); | ||
|
||
const field1 = key.field(1); | ||
|
||
const mySlice = key.slice({ | ||
fields: {}, | ||
}); | ||
|
||
test('slice', () => { | ||
type MySlice = typeof mySlice; | ||
|
||
type MySliceName = InferSliceName<MySlice>; | ||
type MySliceDepNames = InferSliceDepNames<MySlice>; | ||
|
||
() => { | ||
type Result = IfEquals<MySliceName, 'myKey', 'yes', 'no'>; | ||
const result: Result = 'yes'; | ||
}; | ||
|
||
() => { | ||
type Result = IfEquals<MySliceDepNames, 'myKey', 'yes', 'no'>; | ||
let result1: Result = 'no'; | ||
}; | ||
|
||
() => { | ||
type Result = IfEquals<MySliceDepNames, 'dep1Key' | 'dep2Key', 'yes', 'no'>; | ||
let result1: Result = 'yes'; | ||
}; | ||
|
||
expect(1).toBe(1); | ||
}); | ||
|
||
test('key', () => { | ||
type KeyType = typeof key; | ||
|
||
type InferName<T> = T extends Key<infer Name, any> ? Name : never; | ||
type InferDepNames<T> = T extends Key<any, infer DepNames> ? DepNames : never; | ||
type DepName = InferDepNames<KeyType>; | ||
|
||
() => { | ||
type Result = IfEquals<InferName<KeyType>, 'myKey', 'yes', 'no'>; | ||
const result: Result = 'yes'; | ||
}; | ||
|
||
() => { | ||
type Result = IfEquals<DepName, 'myKey', 'yes', 'no'>; | ||
let result1: Result = 'no'; | ||
}; | ||
|
||
() => { | ||
type Result = IfEquals<DepName, 'dep1Key' | 'dep2Key', 'yes', 'no'>; | ||
let result1: Result = 'yes'; | ||
}; | ||
|
||
expect(1).toBe(1); | ||
}); |
This file was deleted.
Oops, something went wrong.
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
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,19 @@ | ||
import { Slice } from '@nalanda/core'; | ||
|
||
export type IfEquals<T, U, Y = unknown, N = never> = (<G>() => G extends T | ||
? 1 | ||
: 2) extends <G>() => G extends U ? 1 : 2 | ||
? Y | ||
: N; | ||
|
||
export const expectType = <Expected, Actual>( | ||
_actual: IfEquals<Actual, Expected, Actual>, | ||
) => void 0; | ||
|
||
export type InferSliceName<T> = T extends Slice<any, infer Name, any> | ||
? Name | ||
: never; | ||
|
||
export type InferSliceDepNames<T> = T extends Slice<any, any, infer DepNames> | ||
? DepNames | ||
: never; |
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
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
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
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
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
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,42 @@ | ||
/* eslint-disable @typescript-eslint/no-duplicate-type-constituents */ | ||
import { describe, test } from '@jest/globals'; | ||
import { IfSubset, expectType } from '../types'; | ||
|
||
describe('IfSubset', () => { | ||
test('works', () => { | ||
expectType< | ||
IfSubset<'kushan', 'kushan' | 'joshi' | 'test', 'yes', 'no'>, | ||
'yes' | ||
>('yes'); | ||
expectType<IfSubset<'kushan', 'kushan' | 'joshi', 'yes', 'no'>, 'yes'>( | ||
'yes', | ||
); | ||
expectType<IfSubset<'kushan', 'kushan', 'yes', 'no'>, 'yes'>('yes'); | ||
|
||
expectType<IfSubset<'kushan', '', 'yes', 'no'>, 'no'>('no'); | ||
expectType< | ||
IfSubset<'kushan' | 'joshi', 'kushan' | 'c' | 'joshi' | 'c', 'yes', 'no'>, | ||
'yes' | ||
>('yes'); | ||
|
||
expectType< | ||
IfSubset< | ||
'kushan' | 'joshi' | 'k', | ||
'kushan' | 'c' | 'joshi' | 'c', | ||
'yes', | ||
'no' | ||
>, | ||
'no' | ||
>('no'); | ||
|
||
expectType< | ||
IfSubset< | ||
'kushan' | 'joshi' | 'k', | ||
'kushan' | 'c' | 'joshi' | 'c' | 'k', | ||
'yes', | ||
'no' | ||
>, | ||
'yes' | ||
>('yes'); | ||
}); | ||
}); |
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
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
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
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
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
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
Oops, something went wrong.