Skip to content

Commit

Permalink
Use recommended TS settings for libraries (#1230)
Browse files Browse the repository at this point in the history
  • Loading branch information
nvie authored Feb 20, 2025
1 parent 322dc7e commit c18bd5f
Show file tree
Hide file tree
Showing 37 changed files with 174 additions and 160 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## [Unreleased]

- Use `node16` module resolution setting (recommended setting for libraries that run in
both browsers and Node environments)

## [2.6.0] - 2025-02-04

- Implement the [Standard Schema](https://standardschema.dev/) specification.
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
},
"type": "module",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.cts",
"exports": {
".": {
Expand Down
4 changes: 2 additions & 2 deletions src/arrays.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Annotation, Decoder, DecodeResult, DecoderType } from '~/core';
import { annotate, define } from '~/core';
import type { Annotation, Decoder, DecodeResult, DecoderType } from '~/core/index.js';
import { annotate, define } from '~/core/index.js';

/**
* Accepts any array, but doesn't validate its items further.
Expand Down
10 changes: 5 additions & 5 deletions src/basics.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { Decoder } from '~/core';
import { define } from '~/core';
import { quote } from '~/lib/text';
import type { Scalar } from '~/lib/types';
import type { Decoder } from '~/core/index.js';
import { define } from '~/core/index.js';
import { quote } from '~/lib/text.js';
import type { Scalar } from '~/lib/types.js';

import { either } from './unions';
import { either } from './unions.js';

function lazyval<V>(value: (() => V) | V): V {
return typeof value === 'function' ? (value as () => V)() : value;
Expand Down
4 changes: 2 additions & 2 deletions src/booleans.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Decoder } from '~/core';
import { define } from '~/core';
import type { Decoder } from '~/core/index.js';
import { define } from '~/core/index.js';

/**
* Accepts and returns booleans.
Expand Down
10 changes: 5 additions & 5 deletions src/collections.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { Annotation, Decoder } from '~/core';
import { annotate, annotateObject, formatShort, merge } from '~/core';
import { quote } from '~/lib/text';
import type { Annotation, Decoder } from '~/core/index.js';
import { annotate, annotateObject, formatShort, merge } from '~/core/index.js';
import { quote } from '~/lib/text.js';

import { array } from './arrays';
import { pojo } from './objects';
import { array } from './arrays.js';
import { pojo } from './objects.js';

/**
* Accepts objects where all values match the given decoder, and returns the
Expand Down
14 changes: 7 additions & 7 deletions src/core/Decoder.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { Annotation } from './annotate';
import { annotate, isAnnotation } from './annotate';
import type { Formatter } from './format';
import { formatAsIssues, formatInline } from './format';
import type { Result } from './Result';
import { err as makeErr, ok as makeOk } from './Result';
import type { StandardSchemaV1 } from './standard-schema';
import type { Annotation } from './annotate.js';
import { annotate, isAnnotation } from './annotate.js';
import type { Formatter } from './format.js';
import { formatAsIssues, formatInline } from './format.js';
import type { Result } from './Result.js';
import { err as makeErr, ok as makeOk } from './Result.js';
import type { StandardSchemaV1 } from './standard-schema.js';

export type DecodeResult<T> = Result<T, Annotation>;

Expand Down
2 changes: 1 addition & 1 deletion src/core/annotate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isPojo } from '~/lib/utils';
import { isPojo } from '~/lib/utils.js';

const _register = new WeakSet<Annotation>();

Expand Down
8 changes: 4 additions & 4 deletions src/core/format.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { INDENT, indent, isMultiline, quote } from '~/lib/text';
import { isDate } from '~/lib/utils';
import { INDENT, indent, isMultiline, quote } from '~/lib/text.js';
import { isDate } from '~/lib/utils.js';

import type {
Annotation,
ArrayAnnotation,
ObjectAnnotation,
OpaqueAnnotation,
} from './annotate';
import type { StandardSchemaV1 as Std } from './standard-schema';
} from './annotate.js';
import type { StandardSchemaV1 as Std } from './standard-schema.js';

export type Formatter = (err: Annotation) => string | Error;

Expand Down
8 changes: 4 additions & 4 deletions src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './annotate';
export * from './Decoder';
export * from './format';
export * from './Result';
export * from './annotate.js';
export * from './Decoder.js';
export * from './format.js';
export * from './Result.js';
10 changes: 5 additions & 5 deletions src/dates.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { Decoder } from '~/core';
import { define } from '~/core';
import { isDate } from '~/lib/utils';
import type { Decoder } from '~/core/index.js';
import { define } from '~/core/index.js';
import { isDate } from '~/lib/utils.js';

import { regex } from './strings';
import { either } from './unions';
import { regex } from './strings.js';
import { either } from './unions.js';

// Only matches the shape. This "over-matches" some values that still aren't
// valid dates (like 9999-99-99), but those will be caught by JS Date's
Expand Down
60 changes: 33 additions & 27 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
// All built-in decoders
export { array, nonEmptyArray, poja, tuple } from './arrays';
export { always, constant, fail, never, unknown } from './basics';
export { null_, nullable, nullish, optional, undefined_ } from './basics';
export { boolean, truthy } from './booleans';
export { mapping, record, set, setFromArray } from './collections';
export { date, datelike, iso8601 } from './dates';
export type { JSONArray, JSONObject, JSONValue } from './json';
export { json, jsonArray, jsonObject } from './json';
export type { SizeOptions } from './lib/size-options';
export { instanceOf, lazy, prep } from './misc';
export { anyNumber, integer, number, positiveInteger, positiveNumber } from './numbers';
export { bigint } from './numbers';
export { exact, inexact, object, pojo } from './objects';
export { endsWith, nonEmptyString, regex, startsWith, string } from './strings';
export { identifier, nanoid, uuid, uuidv1, uuidv4 } from './strings';
export { email, httpsUrl, url } from './strings';
export { decimal, hexadecimal, numeric } from './strings';
export { either, enum_, oneOf, select, taggedUnion } from './unions';
export { array, nonEmptyArray, poja, tuple } from './arrays.js';
export { always, constant, fail, never, unknown } from './basics.js';
export { null_, nullable, nullish, optional, undefined_ } from './basics.js';
export { boolean, truthy } from './booleans.js';
export { mapping, record, set, setFromArray } from './collections.js';
export { date, datelike, iso8601 } from './dates.js';
export type { JSONArray, JSONObject, JSONValue } from './json.js';
export { json, jsonArray, jsonObject } from './json.js';
export type { SizeOptions } from './lib/size-options.js';
export { instanceOf, lazy, prep } from './misc.js';
export {
anyNumber,
integer,
number,
positiveInteger,
positiveNumber,
} from './numbers.js';
export { bigint } from './numbers.js';
export { exact, inexact, object, pojo } from './objects.js';
export { endsWith, nonEmptyString, regex, startsWith, string } from './strings.js';
export { identifier, nanoid, uuid, uuidv1, uuidv4 } from './strings.js';
export { email, httpsUrl, url } from './strings.js';
export { decimal, hexadecimal, numeric } from './strings.js';
export { either, enum_, oneOf, select, taggedUnion } from './unions.js';

// Core functionality
export type { Decoder, DecodeResult, DecoderType } from '~/core';
export type { Err, Ok, Result } from '~/core';
export type { Formatter } from '~/core';
export { define } from '~/core';
export { err, ok } from '~/core';
export { formatInline, formatShort } from '~/core';
export type { Scalar } from '~/lib/types';
export type { Decoder, DecodeResult, DecoderType } from '~/core/index.js';
export type { Err, Ok, Result } from '~/core/index.js';
export type { Formatter } from '~/core/index.js';
export { define } from '~/core/index.js';
export { err, ok } from '~/core/index.js';
export { formatInline, formatShort } from '~/core/index.js';
export type { Scalar } from '~/lib/types.js';

// Deprecated aliases (will get removed in a future version)
export { hardcoded, maybe, mixed } from './basics';
export { dict } from './collections';
export { hardcoded, maybe, mixed } from './basics.js';
export { dict } from './collections.js';
18 changes: 9 additions & 9 deletions src/json.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { Decoder } from '~/core';
import type { Decoder } from '~/core/index.js';

import { array } from './arrays';
import { null_ } from './basics';
import { boolean } from './booleans';
import { record } from './collections';
import { lazy } from './misc';
import { number } from './numbers';
import { string } from './strings';
import { either } from './unions';
import { array } from './arrays.js';
import { null_ } from './basics.js';
import { boolean } from './booleans.js';
import { record } from './collections.js';
import { lazy } from './misc.js';
import { number } from './numbers.js';
import { string } from './strings.js';
import { either } from './unions.js';

export type JSONValue = null | string | number | boolean | JSONObject | JSONArray;
export type JSONObject = { [key: string]: JSONValue | undefined };
Expand Down
4 changes: 2 additions & 2 deletions src/misc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Decoder } from '~/core';
import { annotate, define } from '~/core';
import type { Decoder } from '~/core/index.js';
import { annotate, define } from '~/core/index.js';

// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
export interface Klass<T> extends Function {
Expand Down
6 changes: 3 additions & 3 deletions src/numbers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Decoder } from '~/core';
import { define } from '~/core';
import { isBigInt, isNumber } from '~/lib/utils';
import type { Decoder } from '~/core/index.js';
import { define } from '~/core/index.js';
import { isBigInt, isNumber } from '~/lib/utils.js';

/**
* Accepts any valid ``number`` value.
Expand Down
10 changes: 5 additions & 5 deletions src/objects.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import type { Annotation, Decoder, DecodeResult, DecoderType } from '~/core';
import { annotateObject, define, merge, updateText } from '~/core';
import { difference } from '~/lib/set-methods';
import { quote } from '~/lib/text';
import { isPojo } from '~/lib/utils';
import type { Annotation, Decoder, DecodeResult, DecoderType } from '~/core/index.js';
import { annotateObject, define, merge, updateText } from '~/core/index.js';
import { difference } from '~/lib/set-methods.js';
import { quote } from '~/lib/text.js';
import { isPojo } from '~/lib/utils.js';

type RequiredKeys<T extends object> = {
[K in keyof T]: undefined extends T[K] ? never : K;
Expand Down
16 changes: 8 additions & 8 deletions src/strings.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { Decoder } from '~/core';
import { define } from '~/core';
import type { SizeOptions } from '~/lib/size-options';
import { bySizeOptions } from '~/lib/size-options';
import { isString } from '~/lib/utils';

import { instanceOf } from './misc';
import { either } from './unions';
import type { Decoder } from '~/core/index.js';
import { define } from '~/core/index.js';
import type { SizeOptions } from '~/lib/size-options.js';
import { bySizeOptions } from '~/lib/size-options.js';
import { isString } from '~/lib/utils.js';

import { instanceOf } from './misc.js';
import { either } from './unions.js';

/** Match groups in this regex:
* \1 - the scheme
Expand Down
14 changes: 7 additions & 7 deletions src/unions.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { Annotation, Decoder, DecoderType } from '~/core';
import { define, summarize } from '~/core';
import { indent, quote } from '~/lib/text';
import type { Scalar } from '~/lib/types';
import { isNumber, isString } from '~/lib/utils';
import type { Annotation, Decoder, DecoderType } from '~/core/index.js';
import { define, summarize } from '~/core/index.js';
import { indent, quote } from '~/lib/text.js';
import type { Scalar } from '~/lib/types.js';
import { isNumber, isString } from '~/lib/utils.js';

import { prep } from './misc';
import { object } from './objects';
import { prep } from './misc.js';
import { object } from './objects.js';

const EITHER_PREFIX = 'Either:\n';

Expand Down
10 changes: 8 additions & 2 deletions test-d/inference.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import type { Decoder, DecoderType, JSONValue, JSONObject, JSONArray } from '../dist';
import type {
Decoder,
DecoderType,
JSONValue,
JSONObject,
JSONArray,
} from '../dist/index.js';
import {
// Decoders
always,
Expand Down Expand Up @@ -70,7 +76,7 @@ import {
// Formatters
formatInline,
formatShort,
} from '../dist';
} from '../dist/index.js';
import { expectError, expectType, expectAssignable } from 'tsd';

// Helper function to "test" a decoder on some input, and assert the return type
Expand Down
2 changes: 1 addition & 1 deletion test-d/standard-schema.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { string, numeric } from '../dist';
import { string, numeric } from '../dist/index.js';
import { expectAssignable, expectError, expectType } from 'tsd';
import type { StandardSchemaV1 } from '@standard-schema/spec';

Expand Down
10 changes: 5 additions & 5 deletions test/arrays.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { describe, expect, test } from 'vitest';

import { array, nonEmptyArray, tuple } from '~/arrays';
import { setFromArray } from '~/collections';
import { number } from '~/numbers';
import { object } from '~/objects';
import { string } from '~/strings';
import { array, nonEmptyArray, tuple } from '~/arrays.js';
import { setFromArray } from '~/collections.js';
import { number } from '~/numbers.js';
import { object } from '~/objects.js';
import { string } from '~/strings.js';

describe('array', () => {
test('empty array', () => {
Expand Down
6 changes: 3 additions & 3 deletions test/basics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import {
optional,
undefined_,
unknown,
} from '~/basics';
import { string } from '~/strings';
} from '~/basics.js';
import { string } from '~/strings.js';

import { INPUTS } from './_fixtures';
import { INPUTS } from './_fixtures.js';

describe('null_', () => {
const decoder = null_;
Expand Down
4 changes: 2 additions & 2 deletions test/booleans.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { partition } from 'itertools';
import { describe, expect, test } from 'vitest';

import { boolean, truthy } from '~/booleans';
import { boolean, truthy } from '~/booleans.js';

import { INPUTS } from './_fixtures';
import { INPUTS } from './_fixtures.js';

describe('booleans', () => {
const decoder = boolean;
Expand Down
10 changes: 5 additions & 5 deletions test/core/Decoder.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { describe, expect, test } from 'vitest';

import { always } from '~/basics';
import { annotate, define, formatInline, formatShort } from '~/core';
import { number, positiveInteger } from '~/numbers';
import { pojo } from '~/objects';
import { string } from '~/strings';
import { always } from '~/basics.js';
import { annotate, define, formatInline, formatShort } from '~/core/index.js';
import { number, positiveInteger } from '~/numbers.js';
import { pojo } from '~/objects.js';
import { string } from '~/strings.js';

test('.decode', () => {
// .decode() is tested implicitly because it's used _everywhere_
Expand Down
2 changes: 1 addition & 1 deletion test/core/Result.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, test } from 'vitest';

import { err, ok } from '~/core';
import { err, ok } from '~/core/index.js';

describe('Result', () => {
const r1 = ok(42);
Expand Down
Loading

0 comments on commit c18bd5f

Please sign in to comment.