Skip to content

Commit e14d68a

Browse files
committed
feat(types): OwnPropertyKey
Signed-off-by: Lexus Drumgold <[email protected]>
1 parent 456be69 commit e14d68a

20 files changed

+114
-43
lines changed

__tests__/setup/matchers/descriptor.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
import type { PropertyDescriptor } from '#src/interfaces'
7-
import type { Optional } from '#src/types'
7+
import type { Optional, OwnPropertyKey } from '#src/types'
88
import type { MatcherState, SyncExpectationResult } from '@vitest/expect'
99
import isEqual from 'lodash.isequal'
1010

@@ -18,14 +18,14 @@ import isEqual from 'lodash.isequal'
1818
* @this {MatcherState}
1919
*
2020
* @param {unknown} target - Value to look for `key` in
21-
* @param {string | symbol} key - Property to check
21+
* @param {OwnPropertyKey} key - Property to check
2222
* @param {PropertyDescriptor<T>} descriptor - Expected property descriptor
2323
* @return {SyncExpectationResult} Expectation result
2424
*/
2525
function descriptor<T = any>(
2626
this: MatcherState,
2727
target: unknown,
28-
key: string | symbol,
28+
key: OwnPropertyKey,
2929
descriptor: PropertyDescriptor<T>
3030
): SyncExpectationResult {
3131
/**

src/types/__tests__/get.spec-d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ describe('unit-d:types/Get', () => {
202202
})
203203
})
204204

205-
describe('K extends string | symbol', () => {
205+
describe('K extends OwnPropertyKey', () => {
206206
it('should equal F if K cannot index T', () => {
207207
// Arrange
208208
type K = typeof opaque | 'vrm'

src/types/__tests__/has-key.spec-d.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type Numeric from '../numeric'
1515
import type NegativeNumeric from '../numeric-negative'
1616
import type { tag as opaque } from '../opaque'
1717
import type PropertyKey from '../property-key'
18+
import type OwnPropertyKey from '../property-key-own'
1819
import type Stringify from '../stringify'
1920

2021
describe('unit-d:types/HasKey', () => {
@@ -175,7 +176,7 @@ describe('unit-d:types/HasKey', () => {
175176
type T = string
176177

177178
it('should equal false if K cannot index T', () => {
178-
expectTypeOf<TestSubject<T, string | symbol>>().toEqualTypeOf<false>()
179+
expectTypeOf<TestSubject<T, OwnPropertyKey>>().toEqualTypeOf<false>()
179180
})
180181

181182
it('should equal true if K is explicitly defined on T', () => {
@@ -246,7 +247,7 @@ describe('unit-d:types/HasKey', () => {
246247
type T = Vehicle[]
247248

248249
it('should equal false if K cannot index T', () => {
249-
expectTypeOf<TestSubject<T, string | symbol>>().toEqualTypeOf<false>()
250+
expectTypeOf<TestSubject<T, OwnPropertyKey>>().toEqualTypeOf<false>()
250251
})
251252

252253
it('should equal true if K is explicitly defined on T', () => {

src/types/__tests__/omit-index-signature.spec-d.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type Objectify from '../objectify'
1313
import type Omit from '../omit'
1414
import type TestSubject from '../omit-index-signature'
1515
import type PropertyKey from '../property-key'
16+
import type OwnPropertyKey from '../property-key-own'
1617
import type Stringify from '../stringify'
1718
import type Times from '../times'
1819

@@ -44,9 +45,9 @@ describe('unit-d:types/OmitIndexSignature', () => {
4445
type T = {
4546
[x: Join<['data', string], Dot>]: string
4647
[x: Numeric]: number
48+
[x: OwnPropertyKey]: any
4749
[x: Stringify<bigint>]: string
4850
[x: number]: number
49-
[x: string | symbol]: any
5051
hello: 'world'
5152
foo?: 'bar'
5253
}
@@ -181,7 +182,7 @@ describe('unit-d:types/OmitIndexSignature', () => {
181182
describe('T extends object', () => {
182183
it('should remove index signatures from T', () => {
183184
// Arrange
184-
type T = string & { [x: string | symbol]: any }
185+
type T = string & { [x: OwnPropertyKey]: any }
185186
type Expect = Omit<Objectify<T>, PropertyKey>
186187

187188
// Expect
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @file Type Tests - OwnPropertyKey
3+
* @module tutils/types/tests/unit-d/OwnPropertyKey
4+
*/
5+
6+
import type TestSubject from '../property-key-own'
7+
8+
describe('unit-d:types/OwnPropertyKey', () => {
9+
it('should extract string', () => {
10+
expectTypeOf<TestSubject>().extract<string>().not.toBeNever()
11+
})
12+
13+
it('should extract symbol', () => {
14+
expectTypeOf<TestSubject>().extract<symbol>().not.toBeNever()
15+
})
16+
})

src/types/__tests__/spread.spec-d.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type Numeric from '../numeric'
1313
import type Objectify from '../objectify'
1414
import type { tag as opaque } from '../opaque'
1515
import type Optional from '../optional'
16+
import type OwnPropertyKey from '../property-key-own'
1617
import type TestSubject from '../spread'
1718
import type Writable from '../writable'
1819

@@ -34,10 +35,10 @@ describe('unit-d:types/Spread', () => {
3435
})
3536

3637
describe('IsAny<T> extends true', () => {
37-
it('should equal { [x: string | symbol]: T }', () => {
38+
it('should equal { [x: OwnPropertyKey]: T }', () => {
3839
// Arrange
3940
type T = any
40-
type Expect = { [x: string | symbol]: T }
41+
type Expect = { [x: OwnPropertyKey]: T }
4142

4243
// Expect
4344
expectTypeOf<TestSubject<T>>().toEqualTypeOf<Expect>()
@@ -53,9 +54,9 @@ describe('unit-d:types/Spread', () => {
5354
})
5455

5556
describe('IsEqual<T, object> extends true', () => {
56-
it('should equal { [x: string | symbol]: any }', () => {
57+
it('should equal { [x: OwnPropertyKey]: any }', () => {
5758
// Arrange
58-
type Expect = { [x: string | symbol]: any }
59+
type Expect = { [x: OwnPropertyKey]: any }
5960

6061
// Expect
6162
expectTypeOf<TestSubject<object>>().toEqualTypeOf<Expect>()

src/types/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ export type { default as Predicate } from './predicate'
159159
export type { default as Primitive } from './primitive'
160160
export type { default as Promisable } from './promisable'
161161
export type { default as PropertyKey } from './property-key'
162+
export type { default as OwnPropertyKey } from './property-key-own'
162163
export type { default as NaturalRange } from './range-natural'
163164
export type { default as Remap } from './remap'
164165
export type { default as Reverse } from './reverse'

src/types/object-plain.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
*/
55

66
import type Primitive from './primitive'
7+
import type OwnPropertyKey from './property-key-own'
78

89
/**
910
* A plain old JavaScript object (POJO).
1011
*
1112
* @see https://masteringjs.io/tutorials/fundamentals/pojo
1213
*/
1314
type ObjectPlain = {
14-
[x: string | symbol]: Primitive | object
15+
[x: OwnPropertyKey]: Primitive | object
1516
readonly [Symbol.hasInstance]?: never
1617
}
1718

src/types/omit-index-signature.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import type Objectify from './objectify'
1212
* @example
1313
* type X = OmitIndexSignature<{
1414
* [x: Numeric]: number
15+
* [x: OwnPropertyKey]: bigint
1516
* [x: Stringify<bigint>]: bigint
16-
* [x: number]: number
17-
* [x: string | symbol]: any
1817
* [x: `data.${string}`]: string
18+
* [x: number]: number
1919
* hello: 'world'
2020
* foo?: 'bar'
2121
* }>

src/types/property-key-own.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @file Type Definitions - OwnPropertyKey
3+
* @module tutils/types/OwnPropertyKey
4+
*/
5+
6+
import type PropertyKey from './property-key'
7+
8+
/**
9+
* An own property key.
10+
*
11+
* The own properties of an object are those that are defined directly on that
12+
* object, and are not inherited from the object's prototype.
13+
*
14+
* @see {@linkcode PropertyKey}
15+
*/
16+
type OwnPropertyKey = Exclude<PropertyKey, number>
17+
18+
export type { OwnPropertyKey as default }

src/types/property-key.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
import type NumberString from './number-string'
77

88
/**
9-
* Property key types.
9+
* A property key.
1010
*/
11-
type PropertyKey = NumberString | symbol
11+
type PropertyKey = Extract<NumberString | symbol, any>
1212

1313
export type { PropertyKey as default }

src/types/spread.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type Keyof from './keyof'
1515
import type Length from './length'
1616
import type Objectify from './objectify'
1717
import type Primitive from './primitive'
18+
import type OwnPropertyKey from './property-key-own'
1819
import type Split from './split'
1920
import type Stringify from './stringify'
2021
import type Writable from './writable'
@@ -41,9 +42,9 @@ type Spread<T> = IsNever<T> extends true
4142
: T extends unknown
4243
? Objectify<
4344
IsAny<T> extends true
44-
? Record<string | symbol, T>
45+
? Record<OwnPropertyKey, T>
4546
: IsEqual<T, object> extends true
46-
? Record<string | symbol, any>
47+
? Record<OwnPropertyKey, any>
4748
: T extends string
4849
? Split<T, EmptyString>
4950
: T extends Primitive

src/utils/assign-with.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@
33
* @module tutils/utils/assignWith
44
*/
55

6-
import type { Assign, Fn, ObjectCurly, Objectify } from '#src/types'
6+
import type {
7+
Assign,
8+
Fn,
9+
ObjectCurly,
10+
Objectify,
11+
OwnPropertyKey
12+
} from '#src/types'
713
import cast from './cast'
814
import clone from './clone'
915
import define from './define'
@@ -15,10 +21,10 @@ import properties from './properties'
1521
*
1622
* @param {any} outgoing - Outgoing property value
1723
* @param {any} incoming - Incoming property value from source object
18-
* @param {string | symbol} key - Current property key
24+
* @param {OwnPropertyKey} key - Current property key
1925
* @return {any} New property value
2026
*/
21-
type AssignCustomizer = Fn<[any, any, string | symbol]>
27+
type AssignCustomizer = Fn<[any, any, OwnPropertyKey]>
2228

2329
/**
2430
* Assigns own properties of one or more `source` objects to a target object.

src/utils/equal.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type {
77
Fn,
88
Nilable,
99
Objectify,
10+
OwnPropertyKey,
1011
PropertyKey,
1112
TypedArray
1213
} from '#src/types'
@@ -57,9 +58,9 @@ const equal = <T, U, H = PropertyKey>(
5758
* Returns an array containing own properties.
5859
*
5960
* @param {Objectify<any>} obj - Target object
60-
* @return {(string | symbol)[]} Own properties array
61+
* @return {OwnPropertyKey[]} Own properties array
6162
*/
62-
const owned = (obj: Objectify<any>): (string | symbol)[] => {
63+
const owned = (obj: Objectify<any>): OwnPropertyKey[] => {
6364
return [
6465
...Object.getOwnPropertySymbols(obj),
6566
...Object.getOwnPropertyNames(obj)
@@ -154,9 +155,9 @@ const equal = <T, U, H = PropertyKey>(
154155
/**
155156
* Own properties of {@linkcode a}.
156157
*
157-
* @const {(string | symbol)[]} properties
158+
* @const {OwnPropertyKey[]} properties
158159
*/
159-
const properties: (string | symbol)[] = owned(a)
160+
const properties: OwnPropertyKey[] = owned(a)
160161

161162
// compare property values
162163
switch (true) {

src/utils/ksort.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
*/
55

66
import type { PropertyDescriptor } from '#src/interfaces'
7-
import type { Nilable, ObjectCurly, Objectify } from '#src/types'
7+
import type {
8+
Nilable,
9+
ObjectCurly,
10+
Objectify,
11+
OwnPropertyKey
12+
} from '#src/types'
813
import alphabetize from './alphabetize'
914
import cast from './cast'
1015
import define from './define'
@@ -96,9 +101,9 @@ const ksort = <T extends Objectify<any>>(
96101
/**
97102
* Property names mapped to property descriptors.
98103
*
99-
* @const {[string | symbol, PropertyDescriptor][]}
104+
* @const {[OwnPropertyKey, PropertyDescriptor][]}
100105
*/
101-
const props: [string | symbol, PropertyDescriptor][] = [
106+
const props: [OwnPropertyKey, PropertyDescriptor][] = [
102107
...alphabetize(Object.getOwnPropertyNames(object), identity, options),
103108
...Object.getOwnPropertySymbols(object)
104109
].map(key => [key, descriptor(object, key)])

src/utils/merge-with.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@
33
* @module tutils/utils/mergeWith
44
*/
55

6-
import type { Fn, Merge, ObjectCurly, Objectify } from '#src/types'
6+
import type {
7+
Fn,
8+
Merge,
9+
ObjectCurly,
10+
Objectify,
11+
OwnPropertyKey
12+
} from '#src/types'
713
import cast from './cast'
814
import clone from './clone'
915
import define from './define'
@@ -16,10 +22,10 @@ import properties from './properties'
1622
*
1723
* @param {any} outgoing - Outgoing property value
1824
* @param {any} incoming - Incoming property value from source object
19-
* @param {string | symbol} key - Current property key
25+
* @param {OwnPropertyKey} key - Current property key
2026
* @return {any} New property value
2127
*/
22-
type MergeCustomizer = Fn<[any, any, string | symbol]>
28+
type MergeCustomizer = Fn<[any, any, OwnPropertyKey]>
2329

2430
/**
2531
* Recursively merges own properties of one or more `source` objects into a

src/utils/overwrite-with.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @module tutils/utils/overwriteWith
44
*/
55

6-
import type { Fn, ObjectCurly, Overwrite } from '#src/types'
6+
import type { Fn, ObjectCurly, Overwrite, OwnPropertyKey } from '#src/types'
77
import cast from './cast'
88
import clone from './clone'
99
import define from './define'
@@ -16,10 +16,10 @@ import properties from './properties'
1616
*
1717
* @param {any} outgoing - Outgoing property value
1818
* @param {any} incoming - Incoming property value from source object
19-
* @param {string | symbol} key - Current property key
19+
* @param {OwnPropertyKey} key - Current property key
2020
* @return {any} New property value
2121
*/
22-
type OverwriteCustomizer = Fn<[any, any, string | symbol]>
22+
type OverwriteCustomizer = Fn<[any, any, OwnPropertyKey]>
2323

2424
/**
2525
* Assigns own properties from one or more `source` objects to a destination

src/utils/properties.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
* @module tutils/utils/properties
44
*/
55

6-
import type { Nilable, PropertyKey, Stringify } from '#src/types'
6+
import type {
7+
Nilable,
8+
OwnPropertyKey,
9+
PropertyKey,
10+
Stringify
11+
} from '#src/types'
712
import cast from './cast'
813
import enumerable from './enumerable'
914
import type PropertiesOptions from './properties.options'
@@ -14,7 +19,9 @@ import select from './select'
1419
*
1520
* @template K - Property key type
1621
*/
17-
type Properties<K extends PropertyKey> = K extends symbol ? K : Stringify<K>
22+
type Properties<K extends PropertyKey> = K extends OwnPropertyKey
23+
? K
24+
: Stringify<K>
1825

1926
/**
2027
* Returns an array containing own properties of a given target value.

0 commit comments

Comments
 (0)