An open API service indexing awesome lists of open source software.

https://github.com/keift/typof

Infer types.
https://github.com/keift/typof

array boolean date float integer number object string type typeof

Last synced: 13 days ago
JSON representation

Infer types.

Awesome Lists containing this project

README

          

[String]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String
[Number]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number
[Boolean]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean
[Object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object
[Date]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
[Buffer]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer
[Promise]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
[Void]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Undefined
[Null]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/null
[Undefined]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Undefined

[Types]: ./src/types/Types.type.ts













## Contents

- [About](#about)
- [Features](#features)
- [Installation](#installation)
- [Documentation](#documentation)
- [Tree](#tree)
- [Import](#import)
- [Methods](#methods)
- [Types](#types)
- [Links](#links)
- [Discord](https://discord.gg/keift)
- [Telegram](https://t.me/keiftt)
- [Twitter](https://x.com/keiftttt)
- [GitHub](https://github.com/keift)
- [License](#license)

## About

Infer types.

## Features

- Infers multiple types
- Infers integer and float types
- Infers the type from a string
- There are simple type converters

## Installation

You can install it as follows.

```shell
# NPM
npm add typof

# PNPM
pnpm add typof

# Yarn
yarn add typof

# Bun
bun add typof

# Deno
deno add typof
```

## Documentation

### Tree

Briefly as follows.

```typescript
typof

├── typof(value)
├── string(value)
├── number(value)
├── integer(value)
├── boolean(value)
├── date(value)
├── object(value)
├── array(value)
├── _null(value)
├── _undefined(value)

└── type Types
```

### Import

Briefly as follows.

```typescript
import { typof } from 'typof';
```

### Methods

`typof(value)`

Infer types.

> | Parameter | Type | Default | Description |
> | --------- | ------- | ------- | --------------------- |
> | value | Unknown | | Value to infer types. |
>
> returns [Types]\[]
>
> Example:
>
> ```typescript
> // Tests are as follows. (Is this an integer?)
> if (typof(0).includes('integer')) console.log('This is an integer.');
>
> // Index zero always ensures reliable type checking. As the index increases, species depth also increases.
> if (typof('0.5')[0] === 'string') console.log('This is a string.');
>
> typof('test'); // [ "string" ]
>
> typof('0'); // [ "string", "number", "integer" ]
> typof(0); // [ "number", "integer" ]
>
> typof('0.5'); // [ "string", "number", "float" ]
> typof(0.5); // [ "number", "float" ]
>
> typof('true'); // [ "string", "boolean" ]
> typof(true); // [ "boolean" ]
>
> typof('2025-01-01'); // [ "string", "date" ]
> typof(new Date('2025-01-01')); // [ "object", "date" ]
>
> typof('{"key":"value"}'); // [ "string", "object" ]
> typof({ key: 'value' }); // [ "object" ]
>
> typof('["test"]'); // [ "string", "array" ]
> typof(['test']); // [ "array" ]
>
> typof('null'); // [ "string", "null" ]
> typof(null); // [ "null" ]
>
> typof('undefined'); // [ "string", "undefined" ]
> typof(undefined); // [ "undefined" ]
> ```


`string(value)`

Convert to string.

> | Parameter | Type | Default | Description |
> | --------- | ------- | ------- | ----------------- |
> | value | Unknown | | Value to convert. |
>
> returns [String]
>
> Example:
>
> ```typescript
> string(0.5); // "0.5"
> string(true); // "true"
> string(new Date('2025-01-01')); // "2025-01-01T00:00:00.000Z"
> string({ key: 'value' }); // '{"key":"value"}'
> string(['test']); // '["test"]'
> string(null); // "null"
> string(undefined); // "undefined"
> ```


`number(value)`

Convert to number.

> | Parameter | Type | Default | Description |
> | --------- | ------- | ------- | ----------------- |
> | value | Unknown | | Value to convert. |
>
> returns [Number]
>
> Example:
>
> ```typescript
> number('0.5'); // 0.5
> number(0.5); // 0.5
> number('test'); // NaN
> ```


`integer(value)`

Convert to integer.

> | Parameter | Type | Default | Description |
> | --------- | ------- | ------- | ----------------- |
> | value | Unknown | | Value to convert. |
>
> returns [Number]
>
> Example:
>
> ```typescript
> integer('0.5'); // 0
> integer(0.5); // 0
> integer('test'); // NaN
> ```


`boolean(value)`

Convert to boolean.

> | Parameter | Type | Default | Description |
> | --------- | ------- | ------- | ----------------- |
> | value | Unknown | | Value to convert. |
>
> returns [Boolean] | Value
>
> Example:
>
> ```typescript
> boolean('true'); // true
> boolean(true); // true
> boolean('test'); // "test"
> ```


`date(value)`

Convert to date.

> | Parameter | Type | Default | Description |
> | --------- | ------- | ------- | ----------------- |
> | value | Unknown | | Value to convert. |
>
> returns [Date] | Value
>
> Example:
>
> ```typescript
> date('2025-01-01'); // 2025-01-01T00:00:00.000Z
> date(new Date('2025-01-01')); // 2025-01-01T00:00:00.000Z
> date('test'); // "test"
> ```


`object(value)`

Convert to object.

> | Parameter | Type | Default | Description |
> | --------- | ------- | ------- | ----------------- |
> | value | Unknown | | Value to convert. |
>
> returns [Object] | Value
>
> Example:
>
> ```typescript
> object('{"key":"value"}'); // { key: "value" }
> object('["test"]'); // '["test"]'
> ```


`array(value)`

Convert to array.

> | Parameter | Type | Default | Description |
> | --------- | ------- | ------- | ----------------- |
> | value | Unknown | | Value to convert. |
>
> returns Unknown\[] | Value
>
> Example:
>
> ```typescript
> array('["test"]'); // [ "test" ]
> array('{"key":"value"}'); // '{"key":"value"}'
> ```


`_null(value)`

Convert to null.

> | Parameter | Type | Default | Description |
> | --------- | ------- | ------- | ----------------- |
> | value | Unknown | | Value to convert. |
>
> returns [Null] | Value
>
> Example:
>
> ```typescript
> _null('null'); // null
> _null(null); // null
> _null('test'); // "test"
> ```


`_undefined(value)`

Convert to undefined.

> | Parameter | Type | Default | Description |
> | --------- | ------- | ------- | ----------------- |
> | value | Unknown | | Value to convert. |
>
> returns [Undefined] | Value
>
> Example:
>
> ```typescript
> _undefined('undefined'); // undefined
> _undefined(undefined); // undefined
> _undefined('test'); // "test"
> ```

### Types

| Type |
| ------- |
| [Types] |

## Links

- [Discord](https://discord.gg/keift)
- [Telegram](https://t.me/keiftt)
- [Twitter](https://x.com/keiftttt)
- [GitHub](https://github.com/keift)

## License

MIT License

Copyright (c) 2025 Keift

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.