diff --git a/README.md b/README.md index 91abc59a..eca7d419 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ - [`fn(node[, index][, parent])`](#fnnode-index-parent) - [`Type<[T]>`](#typet) - [`Uint`](#uint) + - [`Value<[T]>`](#valuet) - [Contribute](#contribute) ## What is this? @@ -74,7 +75,8 @@ import type { PositionalInfo, Test, TestFunction, - Type + Type, + Value } from '@flex-development/unist-util-types' ``` @@ -252,6 +254,15 @@ Range: `[0, 10]` > **source**: [`src/uint.ts`](src/uint.ts) +### `Value<[T]>` + +Extract the `value` of [*tree*][tree] `T`. + +- `T` ([**`Node`**][node]): tree to extract value from + - **default**: [`Literal`][node] + +> **source**: [`src/value.ts`](src/value.ts) + ## Contribute See [`CONTRIBUTING.md`](CONTRIBUTING.md). diff --git a/src/__tests__/value.spec-d.ts b/src/__tests__/value.spec-d.ts new file mode 100644 index 00000000..356f2156 --- /dev/null +++ b/src/__tests__/value.spec-d.ts @@ -0,0 +1,17 @@ +/** + * @file Type Tests - Value + * @module unist-util-types/tests/unit-d/Value + */ + +import type * as mdast from 'mdast' +import type TestSubject from '../value' + +describe('unit-d:Value', () => { + it('should equal T["value"]', () => { + // Arrange + type T = mdast.Text + + // Expect + expectTypeOf>().toEqualTypeOf() + }) +}) diff --git a/src/index.ts b/src/index.ts index fffdc7ef..57f95f46 100644 --- a/src/index.ts +++ b/src/index.ts @@ -22,3 +22,4 @@ export type { default as Test } from './test' export type { default as TestFunction } from './test-function' export type { default as Type } from './type' export type { default as Uint } from './uint' +export type { default as Value } from './value' diff --git a/src/value.ts b/src/value.ts new file mode 100644 index 00000000..dad059bb --- /dev/null +++ b/src/value.ts @@ -0,0 +1,21 @@ +/** + * @file Value + * @module unist-util-types/Value + */ + +import type { Literal, Node } from 'unist' + +/** + * Extract the value of from [*tree*][1] `T`. + * + * [1]: https://github.com/syntax-tree/unist#tree + * + * @see {@linkcode Literal} + * @see {@linkcode Node} + * @see https://github.com/syntax-tree/unist#literal + * + * @template {Node} [T=Literal] - Tree to try extracting value from + */ +type Value = T extends Literal ? T['value'] : never + +export type { Value as default }