import { expectAssignable, expectError, expectType, } from 'tsd'; import * as types from './types'; import * as wbt from './index'; import at from './at'; import {setDelta} from './balance'; import create from './create'; import difference from './difference'; import empty from './empty'; import { ValueExistsError, ValueNotFoundError, ValueOrderError, } from './errors'; import equals from './equals'; import exists from './exists'; import filter from './filter'; import find from './find'; import findAll from './findAll'; import findBy from './findBy'; import findNext from './findNext'; import findNode from './findNode'; import findPrev from './findPrev'; import findWithIndex from './findWithIndex'; import fromDistinctAscArray from './fromDistinctAscArray'; import indexOf from './indexOf'; import insert, { insertIfNotExists, insertOrReplaceIfExists, insertOrThrowIfExists, } from './insert'; import intersection from './intersection'; import isDisjointFrom from './isDisjointFrom'; import isSubsetOf from './isSubsetOf'; import isSupersetOf from './isSupersetOf'; import iterate from './iterate'; import join from './join'; import join2 from './join2'; import map from './map'; import maxValue from './maxValue'; import minValue from './minValue'; import remove, { removeIfExists, removeOrThrowIfNotExists, } from './remove'; import reverseIterate from './reverseIterate'; import setIndex from './setIndex'; import slice from './slice'; import splice, {type SpliceResult} from './splice'; import split, {type SplitResult} from './split'; import splitFirst from './splitFirst'; import splitIndex from './splitIndex'; import splitLast from './splitLast'; import symmetricDifference from './symmetricDifference'; import toArray from './toArray'; import union from './union'; import update, { onConflictKeepTreeValue, onConflictThrowError, onConflictUseGivenValue, onNotFoundDoNothing, onNotFoundThrowError, onNotFoundUseGivenValue, } from './update'; import updateIndex from './updateIndex'; import validate, { type ValidateResult, } from './validate'; import type { InsertConflictHandler, InsertNotFoundHandler, } from './update'; import zip from './zip'; declare type NSTuple = [number, string]; declare const stringTree: types.ImmutableTree; declare const numberTree: types.ImmutableTree; declare const numberStringMapTree: types.ImmutableTree; declare const nonEmptyNumberTree: types.NonEmptyImmutableTree; declare function areStringsEqual(a: string, b: string): boolean; declare function areNumbersEqual(a: number, b: number): boolean; declare function areNumberAndStringEqual(a: number, b: string): boolean; declare function cmpStrings(a: string, b: string): number; declare function cmpNumbers(a: number, b: number): number; declare function cmpNullableNumbers(a: number | null, b: number | null): number; declare function cmpNumberAndString(a: number, b: string): number; declare function getNumberKeyFromTuple(tuple: NSTuple): number; declare function toString(num: number): string; // Basic usage expectType(empty); expectType(at(stringTree, 0)); expectType(at(stringTree, 0, '')); expectType(at(stringTree, 0, 0)); expectType>(create('')); expectType>(update(numberTree, {key: 0, cmp: cmpNullableNumbers, onConflict: onConflictKeepTreeValue, onNotFound: onNotFoundUseGivenValue})); expectType>(update(stringTree, {key: 0, cmp: cmpNumberAndString, onConflict: onConflictKeepTreeValue, onNotFound: onNotFoundDoNothing})); expectType>(update(stringTree, {key: 0, cmp: cmpNumberAndString, onConflict: onConflictKeepTreeValue, onNotFound: onNotFoundThrowError})); expectType>(insert(stringTree, '', cmpStrings)); expectType>(insert(stringTree, '', cmpStrings, onConflictKeepTreeValue)); expectType>(insert(stringTree, '', cmpStrings, onConflictThrowError)); expectType>(insert(stringTree, '', cmpStrings, onConflictUseGivenValue)); expectType>(insert(stringTree, '', cmpStrings, (a: string, b: string) => a + b)); expectType>(insertIfNotExists(stringTree, '', cmpStrings)); expectType>(insertOrReplaceIfExists(stringTree, '', cmpStrings)); expectType>(insertOrThrowIfExists(stringTree, '', cmpStrings)); expectType>(iterate(stringTree)); expectType>(iterate(stringTree, 0)); expectType>(iterate(stringTree, 0, 10)); expectType>(reverseIterate(stringTree)); expectType>(reverseIterate(stringTree, 0)); expectType>(reverseIterate(stringTree, 0, 10)); expectType(equals(stringTree, stringTree)); expectType(equals(stringTree, stringTree, areStringsEqual)); expectType(equals(numberTree, stringTree, areNumberAndStringEqual)); expectType(exists(stringTree, '', cmpStrings)); expectType>(filter(stringTree, (x: string) => x.startsWith('foo'))); expectType(find(stringTree, '', cmpStrings, '')); expectType>(findAll(stringTree, '', cmpStrings)); expectType(findBy(stringTree, (treeValue: string) => cmpStrings(treeValue, ''), '')); expectType(findNext(stringTree, '', cmpStrings, '')); expectType | null>(findNode(stringTree, '', cmpStrings)); expectType(findPrev(stringTree, '', cmpStrings, '')); expectType<[string, number]>(findWithIndex(stringTree, '', cmpStrings, '')); expectType>(fromDistinctAscArray([''])); expectType(indexOf(numberTree, 1, cmpNumbers)); expectType(isDisjointFrom(stringTree, stringTree, cmpStrings)); expectType(isSubsetOf(stringTree, stringTree, cmpStrings)); expectType(isSupersetOf(stringTree, stringTree, cmpStrings)); expectType>(join(numberTree, 0, numberTree)); expectType>(join2(numberTree, numberTree)); expectType>(map(numberTree, toString)); expectType>(remove(stringTree, '', cmpStrings)); expectType>(removeIfExists(stringTree, '', cmpStrings)); expectType>(removeOrThrowIfNotExists(stringTree, '', cmpStrings)); expectType>(setIndex(stringTree, 0, 'foo')); expectType>(updateIndex(stringTree, 0, (x: string) => x + 'foo')); expectType>(slice(stringTree, 1, 2)); expectType>(slice(stringTree, 1)); expectType>(slice(stringTree)); expectType>(splice(numberTree, 1, 2, numberTree)); expectType>(split(numberTree, 0, cmpNumbers)); expectType>(splitIndex(numberTree, 0)); expectType<{readonly tree: types.ImmutableTree; readonly value: number}>(splitFirst(nonEmptyNumberTree)); expectType<{readonly tree: types.ImmutableTree; readonly value: number}>(splitLast(nonEmptyNumberTree)); expectType>(toArray(stringTree)); expectType>(union(stringTree, stringTree, cmpStrings)); expectType>(union(stringTree, stringTree, cmpStrings, (v1, v2) => v2)); expectType>(difference(stringTree, stringTree, cmpStrings)); expectType>(intersection(stringTree, stringTree, cmpStrings)); expectType>(symmetricDifference(numberTree, numberTree, cmpNumbers)); expectType>(zip(stringTree, numberTree)); expectType>(validate(stringTree, cmpStrings)); expectType(setDelta(3)); // Value type override expectType(exists(stringTree, 0, cmpNumberAndString)); expectType(find(stringTree, 0, cmpNumberAndString, null)); expectType>(findAll(stringTree, 0, cmpNumberAndString)); expectType(findBy(stringTree, (treeValue: string) => cmpStrings(treeValue, ''), null)); expectType(findNext(stringTree, 0, cmpNumberAndString, null)); expectType(findPrev(stringTree, 0, cmpNumberAndString, null)); expectType<[string | null, number]>(findWithIndex(stringTree, 0, cmpNumberAndString, null)); expectType(indexOf(stringTree, 1, cmpNumberAndString)); expectType>(split(stringTree, 1, cmpNumberAndString)); // Wrong tree type expectError>(create('')); expectError>(insert(stringTree, '', cmpNumbers)); expectError>(insertIfNotExists(stringTree, '', cmpStrings)); expectError>(insertOrReplaceIfExists(stringTree, '', cmpStrings)); expectError>(insertOrThrowIfExists(stringTree, '', cmpStrings)); expectError>(iterate(stringTree)); expectError>(reverseIterate(stringTree)); expectError(equals(stringTree, stringTree, areStringsEqual)); expectError(exists(numberTree, '', cmpStrings)); expectError(find(stringTree, 0, cmpNumbers, 0)); expectError>(findAll(stringTree, 0, cmpNumbers)); expectError(findBy(stringTree, (treeValue: number) => cmpStrings(treeValue, 0), 0)); expectError | null>(findNode(stringTree, '', cmpStrings)); expectError<[number, number]>(findWithIndex(stringTree, 0, cmpNumbers, 0)); expectError>(fromDistinctAscArray([''])); expectError(isDisjointFrom(stringTree, numberTree, cmpStrings)); expectError(isSubsetOf(stringTree, numberTree, cmpStrings)); expectError(isSupersetOf(stringTree, numberTree, cmpStrings)); expectError>(join(stringTree, 0, stringTree)); expectError>(join2(stringTree, stringTree)); expectError>(map(stringTree, toString)); expectError(maxValue(stringTree)); expectError(minValue(stringTree)); expectError>(remove(stringTree, 0, cmpNumbers)); expectError>(removeIfExists(stringTree, 0, cmpNumbers)); expectError>(removeOrThrowIfNotExists(stringTree, 0, cmpNumbers)); expectError>(setIndex(stringTree, 0, 42)); expectError>(updateIndex(stringTree, 0, () => 42)); expectError>(slice(stringTree, 1, 2)); expectError>(splice(stringTree, 1, 2, numberTree)); expectError>(split(stringTree, 1, cmpNumbers)); expectError<{readonly tree: types.ImmutableTree; readonly value: number}>(splitFirst(numberTree)); expectError<{readonly tree: types.ImmutableTree; readonly value: number}>(splitLast(numberTree)); expectError>(toArray(numberTree)); expectError>(union(stringTree, stringTree, cmpNumbers)); expectError>(difference(stringTree, stringTree, cmpNumbers)); expectError>(intersection(stringTree, stringTree, cmpNumbers)); expectError>(symmetricDifference(stringTree, stringTree, cmpNumbers)); expectError>(zip(numberTree, stringTree)); // Wrong comparator function type expectError>(equals(stringTree, stringTree, areNumbersEqual)); expectError(exists(stringTree, '', cmpNumbers)); expectError(find(stringTree, '', cmpNumbers, '')); expectError>(findAll(stringTree, '', cmpNumbers)); expectError(findNext(stringTree, '', cmpNumbers, '')); expectError | null>(findNode(stringTree, '', cmpNumbers)); expectError(findPrev(stringTree, '', cmpNumbers, '')); expectError<[string, number]>(findWithIndex(stringTree, '', cmpNumbers, '')); expectError>(insert(stringTree, '', cmpNumbers)); expectError(isDisjointFrom(stringTree, stringTree, cmpNumbers)); expectError(isSubsetOf(stringTree, stringTree, cmpNumbers)); expectError(isSupersetOf(stringTree, stringTree, cmpNumbers)); expectError>(remove(stringTree, '', cmpNumbers)); expectError>(removeIfExists(stringTree, '', cmpNumbers)); expectError>(removeOrThrowIfNotExists(stringTree, '', cmpNumbers)); expectError>(split(stringTree, '', cmpNumbers)); expectError>(union(stringTree, stringTree, cmpNumbers)); expectError>(difference(stringTree, stringTree, cmpNumbers)); expectError>(intersection(stringTree, stringTree, cmpNumbers)); expectError>(symmetricDifference(stringTree, stringTree, cmpNumbers)); // Wrong 'mapper' function type. expectError>(map(numberTree, (x: string) => parseInt(x, 10))); // Value type override + wrong comparator function type expectError(find(stringTree, 0, cmpStrings, '')); expectError>(findAll(stringTree, 0, cmpStrings)); expectError(findNext(stringTree, 0, cmpStrings, '')); expectError(findPrev(stringTree, 0, cmpStrings, '')); expectError<[string, number]>(findWithIndex(stringTree, 0, cmpStrings, '')); // InsertConflictHandler expectAssignable>((a: string, b: number) => a + String(b)); expectAssignable>(onConflictKeepTreeValue); expectAssignable>(onConflictThrowError); expectAssignable>(onConflictUseGivenValue); // InsertNotFoundHandler expectAssignable>((a: number) => String(a)); expectAssignable>(onNotFoundDoNothing); expectAssignable>(onNotFoundThrowError); expectAssignable>(onNotFoundUseGivenValue); // Wrong 'onNotFound' function type. expectError>(update(stringTree, 0, cmpNumberAndString, onConflictKeepTreeValue, onNotFoundUseGivenValue)); // Error classes expectAssignable(new ValueExistsError('a')); expectAssignable(new ValueNotFoundError('a')); expectAssignable(new ValueOrderError('a', 'b', 'less than')); expectType(wbt.at); expectType(wbt.create); expectType(wbt.difference); expectType(wbt.empty); expectType(wbt.equals); expectType(wbt.exists); expectType(wbt.filter); expectType(wbt.find); expectType(wbt.findAll); expectType(wbt.findBy); expectType(wbt.findNext); expectType(wbt.findNode); expectType(wbt.findPrev); expectType(wbt.findWithIndex); expectType(wbt.fromDistinctAscArray); expectType(wbt.indexOf); expectType(wbt.insert); expectType(wbt.insertIfNotExists); expectType(wbt.insertOrReplaceIfExists); expectType(wbt.insertOrThrowIfExists); expectType(wbt.intersection); expectType(wbt.isDisjointFrom); expectType(wbt.isSubsetOf); expectType(wbt.isSupersetOf); expectType(wbt.iterate); expectType(wbt.join); expectType(wbt.join2); expectType(wbt.map); expectType(wbt.maxValue); expectType(wbt.minValue); expectType(wbt.remove); expectType(wbt.removeIfExists); expectType(wbt.removeOrThrowIfNotExists); expectType(wbt.reverseIterate); expectType(wbt.setDelta); expectType(wbt.setIndex); expectType(wbt.slice); expectType(wbt.splice); expectType(wbt.split); expectType(wbt.splitFirst); expectType(wbt.splitIndex); expectType(wbt.splitLast); expectType(wbt.symmetricDifference); expectType(wbt.toArray); expectType(wbt.union); expectType(wbt.update); expectType(wbt.updateIndex); expectType(wbt.validate); expectType(wbt.zip);