Skip to content

Commit

Permalink
fix types exports
Browse files Browse the repository at this point in the history
  • Loading branch information
rawpixel-vincent committed Mar 2, 2024
1 parent 840d05a commit 5a828fe
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 39 deletions.
File renamed without changes.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
{
"name": "string-literal-list",
"version": "1.0.0",
"version": "1.0.1",
"description": "an array for strings",
"main": "index.js",
"exports": {
".": {
"import": "./index.js",
"require": "./dist/index.cjs"
"require": "./index.cjs"
}
},
"types": "types.d.ts",
"license": "MIT",
"scripts": {
"test": "node stringList.test.js && tsc --project ./jsconfig.json",
"build": "esbuild --target=esnext --keep-names --format=cjs index.js --outfile=dist/index.cjs"
"build": "esbuild --target=esnext --keep-names --format=cjs ./index.js --outfile=./index.cjs"
},
"author": "Vincent Baronnet",
"type": "module",
Expand Down
2 changes: 1 addition & 1 deletion stringList.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict';
/// <reference path="./types.d.ts" />

/**
* @type {Readonly<isl.ISL>}
Expand Down
2 changes: 0 additions & 2 deletions stringList.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

import stringList from './index.js';

let d = stringList('foo', 'bar');
Expand Down
123 changes: 95 additions & 28 deletions types.d.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,73 @@

type StringConcat<
T1 extends string | number | bigint | boolean,
T2 extends string | number | bigint | boolean
T2 extends string | number | bigint | boolean,
> = `${T1}${T2}`;

type MutableMethod = 'push' | 'slice' | 'sort' | 'unshift' | 'shift' | 'copyWithin' | 'pop' | 'fill' | 'splice' | 'reverse';

type ImplementedMethod = 'indexOf' | 'lastIndexOf' | 'mutable' | 'concat' | 'at' | 'withPrefix' | 'withSuffix' | 'toReversed' | 'toSorted';

type SupportedMethod = 'includes' | 'toSpliced' | 'length' | 'find' | 'findIndex' | 'some' | 'every' | 'filter';

type SupportedMethodWithNativeType = 'map' | 'reduce' | 'reduceRight' | 'entries' | 'keys' | 'values' | 'toLocaleString' | 'toString' | 'forEach' | 'flat' | 'flatMap' | 'join';
type MutableMethod =
| 'push'
| 'slice'
| 'sort'
| 'unshift'
| 'shift'
| 'copyWithin'
| 'pop'
| 'fill'
| 'splice'
| 'reverse';

type ImplementedMethod =
| 'indexOf'
| 'lastIndexOf'
| 'mutable'
| 'concat'
| 'at'
| 'withPrefix'
| 'withSuffix'
| 'toReversed'
| 'toSorted';

type SupportedMethod =
| 'includes'
| 'toSpliced'
| 'length'
| 'find'
| 'findIndex'
| 'some'
| 'every'
| 'filter';

type SupportedMethodWithNativeType =
| 'map'
| 'reduce'
| 'reduceRight'
| 'entries'
| 'keys'
| 'values'
| 'toLocaleString'
| 'toString'
| 'forEach'
| 'flat'
| 'flatMap'
| 'join';

declare global {
namespace isl {


interface ISL<T = string | unknown> extends Omit<Array<T>, ImplementedMethod | MutableMethod | SupportedMethod> {
interface ISL<T = string | unknown>
extends Omit<
Array<T>,
ImplementedMethod | MutableMethod | SupportedMethod
> {
toSorted(compareFn?: (a: T, b: T) => number): ISL<T>;
toReversed(): ISL<T>;
concat<PP = T, P extends string = string>(...arg: P[]): (ISL<(Record<P, P>)[keyof (Record<P, P>)] | PP>);
withPrefix<P extends string = string>(prefix: P): T extends string ? ISL<(StringConcat<P, T>)> : never;
withSuffix<P extends string = string>(suffix: P): T extends string ? ISL<(StringConcat<T, P>)> : never;
concat<PP = T, P extends string = string>(
...arg: P[]
): ISL<Record<P, P>[keyof Record<P, P>] | PP>;
withPrefix<P extends string = string>(
prefix: P,
): T extends string ? ISL<StringConcat<P, T>> : never;
withSuffix<P extends string = string>(
suffix: P,
): T extends string ? ISL<StringConcat<T, P>> : never;
mutable(): string[];

at(n: number): T?;
Expand All @@ -35,23 +81,44 @@ declare global {
indexOf<PP = T>(searchElement: PP, fromIndex?: number): number;
lastIndexOf<PP = T>(searchElement: PP, fromIndex?: number): number;

find<PP = T>(predictate: (val: PP extends T ? PP : string, i: number, obj: ISL<T>) => val is PP): T;
find<PP = T>(
predictate: (
val: PP extends T ? PP : string,
i: number,
obj: ISL<T>,
) => val is PP,
): T;
find(predictate: (val?: string, i: number, obj: ISL<T>) => boolean): T;
findIndex<PP = T>(predictate: (val: PP extends T ? PP : string) => boolean): number;
findIndex(predictate: (val?: string, i: number, obj: ISL<T>) => boolean): number;


some<PP = T>(predictate: (val: PP extends T ? PP : string) => boolean): boolean;
every<PP = T>(predictate: (val: PP extends T ? PP : string) => boolean): boolean;

findIndex<PP = T>(
predictate: (val: PP extends T ? PP : string) => boolean,
): number;
findIndex(
predictate: (val?: string, i: number, obj: ISL<T>) => boolean,
): number;

some<PP = T>(
predictate: (val: PP extends T ? PP : string) => boolean,
): boolean;
every<PP = T>(
predictate: (val: PP extends T ? PP : string) => boolean,
): boolean;

// Return Type overrides
filter<S extends T>(predicate: (value: T, index: number, array: ISL<T>) => value is S, thisArg?: any): S[];
filter(predicate: (value: string, index: number, array: ISL<T>) => boolean, thisArg?: any): string[];
toSpliced(start: number, deleteCount: number, ...items: string[]): string[];
filter<S extends T>(
predicate: (value: T, index: number, array: ISL<T>) => value is S,
thisArg?: any,
): S[];
filter(
predicate: (value: string, index: number, array: ISL<T>) => boolean,
thisArg?: any,
): string[];
toSpliced(
start: number,
deleteCount: number,
...items: string[]
): string[];
}
}
}


export { }
export { };

0 comments on commit 5a828fe

Please sign in to comment.