Skip to content

ts options #1024

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"no-sparse-arrays": 0,
"no-unexpected-multiline": 0,
"@typescript-eslint/no-empty-function": 0,
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/no-this-alias": 0,
"@typescript-eslint/no-unused-vars": ["error", {"ignoreRestSiblings": true}]
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"test": "yarn test:typecheck && yarn test:lint && yarn test:mocha",
"test:mocha": "mkdir -p test/output && mocha --conditions=mocha 'test/**/*-test.*' 'test/plot.js'",
"test:lint": "eslint src test",
"test:typecheck": "tsc --noEmit",
"test:typecheck": "tsc --noEmit && tsc --noEmit -p test/tsconfig.json",
"prepublishOnly": "rm -rf dist && rollup -c && tsc",
"postpublish": "git push && git push --tags",
"dev": "vite"
Expand Down Expand Up @@ -63,6 +63,7 @@
"prettier": "^2.7.1",
"rollup": "2",
"rollup-plugin-terser": "7",
"tsd": "^0.22.0",
"tslib": "^2.4.0",
"typescript": "^4.6.4",
"typescript-module-alias": "^1.0.2",
Expand Down
95 changes: 95 additions & 0 deletions src/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
* Values are associated to screen encodings (positions, colors…) via scales.
*/
export type Value = number | string | Date | boolean | null | undefined;

/**
* A data point with values attached to field names; typically, a row from a
* tabular dataset.
*/
export type Row = Record<string, Value>;

/**
* A single Datum is often a Value, a Row, or an array of values; if a Row,
* possible field names can be inferred from its keys to define accessors; if an
* array, typical accessors are indices, and length, expressed as strings
*/
export type Datum = Row | Value | Value[];
export type FieldNames<T> = T extends Row
? // eslint-disable-next-line @typescript-eslint/ban-types
keyof T | (string & {})
: T extends Value[]
? // eslint-disable-next-line @typescript-eslint/ban-types
"length" | "0" | "1" | "2" | (string & {})
: never;

/**
* The marks data; typically an array of Datum, but can also be defined as an
* iterable compatible with Array.from.
*/
export type Data<T extends Datum> = ArrayLike<T> | Iterable<T> | TypedArray;

/**
* An array or typed array constructor, or any class that implements Array.from
*/
export type ArrayType = ArrayConstructor | TypedArrayConstructor;

/**
* The data is then arrayified, and a range of indices is computed, serving as
* pointers into the columnar representation of each channel
*/
export type DataArray<T extends Datum> = T[] | TypedArray;

/**
* A series is an array of indices, used to group data into classes (e.g.,
* groups and facets)
*/
export type index = number; // integer
export type Series = index[] | Uint32Array;
export type Facets = Series[];

export type NumericArray = number[] | TypedArray;
export type ValueArray = NumericArray | Value[];

/**
* Typed arrays are preserved through arrayify
*/
export type TypedArray =
| Int8Array
| Uint8Array
| Int16Array
| Uint16Array
| Int32Array
| Uint32Array
| Uint8ClampedArray
| Float32Array
| Float64Array;

export type TypedArrayConstructor =
| Int8ArrayConstructor
| Uint8ArrayConstructor
| Int16ArrayConstructor
| Uint16ArrayConstructor
| Int32ArrayConstructor
| Uint32ArrayConstructor
| Uint8ClampedArrayConstructor
| Float32ArrayConstructor
| Float64ArrayConstructor;

export type Constructor<T extends TypedArray> = T extends Int8Array
? Int8ArrayConstructor
: T extends Uint8Array
? Uint8ArrayConstructor
: T extends Int16Array
? Int16ArrayConstructor
: T extends Int32Array
? Int32ArrayConstructor
: T extends Uint32Array
? Uint32ArrayConstructor
: T extends Uint8ClampedArray
? Uint8ClampedArrayConstructor
: T extends Float32Array
? Float32ArrayConstructor
: T extends Float64Array
? Float64ArrayConstructor
: never;
Loading