๐ฆ Provides frequently used types for your TypeScript projects. ๐ฆ
npm install @igorskyflyer/common-types
- Features
- API
- KeysOf\
- TypeOfValues\
- MethodsOf\
- PropertiesOf\
- DeepPartial\
- Promisable\
- EnumKeys\
- Func
- Callback
- TrimLeft\
- TrimRight\
- Trim\
- IsGeneric\
- MethodSignature\
- Override\
- HasOverlap\
- Extend\
- MethodName\
- Usage
- API
- Examples
- Changelog
- Support
- License
- Related
- Author
- ๐ Get all keys of a type with KeysOf
- ๐ฆ Extract all value types from top-level properties via TypeOfValues
- ๐ Identify only method keys using MethodsOf
- ๐งพ Identify only property keys using PropertiesOf
- ๐ฟ Make every property (nested too) optional with DeepPartial
- โณ Accept sync or async values using Promisable
- ๐ฏ Find keys whose values match a specific type via EnumKeys
- ๐ Define generic function signatures with Func or Callback
- โ Remove leading/trailing whitespace in string types with TrimLeft, TrimRight, Trim
- โ Detect generic types using IsGeneric
- ๐ Get a methodโs exact signature with MethodSignature
- ๐ Override existing keys with new types via Override
- ๐ซ Prevent extending with overlapping keys using Extend
- ๐ Validate a method exists and return its name with MethodName
Install it by executing any of the following, depending on your preferred package manager:
``bash`
pnpm add @igorskyflyer/common-types
`bash`
yarn add @igorskyflyer/common-types
`bash`
npm i @igorskyflyer/common-types
Extracts all keys from the type Type.
> ๐ก TIP
>
> CAN be used with generics as well.
>
example.ts`ts`
type ArrayKeys = KeysOf
---
Extracts all value types of the type Type. Works with top-level properties only.
IPerson.ts`ts`
interface IPerson {
firstName: string
lastName: string
zip: number
isMember: boolean
}
example.ts`ts`
type ValueTypes = TypeOfValues
---
Extracts all methods from the type Type.
> ๐ CAUTION
>
> Can NOT be used with generics.
>
example.ts`ts`
type NumberMethods = MethodsOf
---
Extracts all properties from the type Type.
> ๐ CAUTION
>
> Can NOT be used with generics.
>
example.ts`ts`
type StringProperties = PropertiesOf
---
Constructs a type with all top-level and nested properties of the type Type set to optional.
> ๐ก TIP
>
> See also TypeScript's built-in utility type Partial
>
IPerson.ts`ts`
interface IPerson {
name: string
address: {
city: string
zip: number
}
}
example.ts`ts
type PersonOptional = DeepPartial
/**
* PersonOptional:
* {
* name?: string
* address?: {
* city?: string
* zip?: number
* }
* }
*/
`
---
Provides a convenient way to allow flexibility in handling values that could either be immediate or asynchronously resolved.
example.ts`ts
const immediateValue: number = 42
const promiseValue: Promisable
async function handleValue(value: Promisable
const result = await processValue(value)
console.log(result) // Will log the number 42, whether value was a direct number or a Promise resolving to 42
}
handleValue(immediateValue)
handleValue(promiseValue)
`
---
Extracts all keys from the Type that are of the type KeyType.
IConfig`ts`
interface IConfig {
apiUrl: string
timeout: number
isEnabled: boolean
retryAttempts: number
}
example.ts`ts`
type ConfigNumbers = EnumKeys
---
Constructs a generic Function-like type with the arguments of type Args and the return value of type FnReturn.
example.ts`ts
function process(items: number[], callback: Func
// shortened for brevity
// do NOT access your Array immediately :)
for (let i = 0; i < items.length; i++) {
if (callback(items[i])) {
return true
}
}
return false
}
process([1, 1, 8, 1], (item) => {
if (item % 2 === 0) {
return true
}
return false
}) // returns true
`
---
Alias of Func
---
Recursively removes all leading whitespace from the String type Input.
example.ts`ts
type Id = ' ID'
type ProperId = TrimLeft
const id: ProperId = ' ID' // ERROR: does NOT accept leading whitespace
`
---
Recursively removes all trailing whitespace from the String type Input.
example.ts`ts
type Id = 'ID '
type ProperId = TrimRight
const id: ProperId = 'ID ' // ERROR: does NOT accept leading whitespace
`
---
Recursively removes all leading and trailing whitespace from the String type Input.
example.ts`ts
type Id = ' ID '
type ProperId = Trim
const id: ProperId = ' ID ' // ERROR: does NOT accept leading nor trailing whitespace
`
> ๐ก TIP
>
> A very cool usage of the Trim type is implemented in the magic-querySelector project.
>
---
Returns a Boolean whether the type Type is a generic.
example.ts`ts`
type ArrayIsGeneric = IsGeneric
type NumberIsGeneric = IsGeneric
---
Gets the method signature Method of the type Type.
example.ts`ts`
type NumberToFixedMethod = MethodSignature
---
Overrides the type Type with the new type of Changes.
IPerson`ts`
interface IPerson {
name: string
children: boolean
}
example.ts`ts
const person: IPerson = {
name:'John Doe',
children: true
}
type NewPerson = Override
const newPerson: NewPerson = {
name:'John Doe',
children: 2
}
`
---
Checks whether the types FirstType and SecondType overlap, i.e. have the same keys.
> โ ๏ธ WARNING
>
> It only checks the key names, NOT their TYPES!
>
IPerson`ts`
interface IPerson {
name: string
children: boolean
}
example.ts`ts`
type PersonOverlap = HasOverlap<
IPerson,
{
name: string
children: boolean
}
> // returns true
---
Extends the type Type with the new type of Changes with only non-existent keys in type Type.
IPerson`ts`
interface IPerson {
name: string
children: number
}
example.ts`tsnever
type NewPerson = Extend
// will return here
const newPerson: NewPerson = {
name: 'John Doe',
children: 2
} // will error
type NewestPerson = Extend
const newestPerson: NewestPerson = {
name: 'John Doe',
children: 2,
profession: 'Developer'
} // will NOT error
`
---
Checks for the existence of the method Method in the type of Type and returns it if found.
example.ts`ts`
type NumberToFixedMethod = MethodName
---
utils.ts`ts
import type { Callback } from '@igorskyflyer/common-types'
function process(
items: number[],
callback: Callback
): boolean {
// shortened for brevity
// do NOT access your Array immediately :)
for (let i = 0; i < items.length; i++) {
if (callback(items[i])) {
return true
}
}
return false
}
const result = process([1, 1, 8, 1], (item) => {
if (item % 2 === 0) {
return true
}
return false
}) // returns true
console.log(result)
``
๐ The changelog is available here, CHANGELOG.md.
Licensed under the MIT license which is available here, MIT license.
> _๐ Provides ways of checking whether a path is a legacy Windows device. ๐พ_
@igorskyflyer/magic-queryselector
> _๐ช A TypeScript-types patch for querySelector/querySelectorAll, make them return types you expect them to! ๐ฎ_
> _๐ถ๏ธ Reads a JSON file into a Map. ๐ป_
> _๐งฌ A lightweight JavaScript utility allowing deep copy-by-value of nested objects, arrays and arrays of objects. ๐ช_
@igorskyflyer/extendable-string
> _๐ฆ ExtendableString allows you to create strings on steroids that have custom transformations applied to them, unlike common, plain strings.. ๐ช_