Skip to content

Commit c90722d

Browse files
author
Don McCurdy
committed
Stricter types, remove remaining types
1 parent 4cd4d27 commit c90722d

File tree

5 files changed

+62
-53
lines changed

5 files changed

+62
-53
lines changed

examples/website/plot/app.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ export default function App({resolution = 200, showAxis = true, equation = EQUAT
2828
equation &&
2929
resolution &&
3030
new PlotLayer({
31-
getPosition: (u, v) => {
31+
getPosition: ([u, v]) => {
3232
const x = (u - 1 / 2) * Math.PI * 2;
3333
const y = (v - 1 / 2) * Math.PI * 2;
3434
return [x, y, equation(x, y)];
3535
},
36-
getColor: (x, y, z) => [40, z * 128 + 128, 160],
36+
getColor: ([x, y, z]) => [40, z * 128 + 128, 160],
3737
getXScale: getScale,
3838
getYScale: getScale,
3939
getZScale: getScale,

examples/website/plot/plot-layer/axes-layer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {Axis, Tick, TickFormat, Vec2, Vec3} from './types';
1414
/* Constants */
1515
const DEFAULT_FONT_SIZE = 48;
1616
const DEFAULT_TICK_COUNT = 6;
17-
const DEFAULT_TICK_FORMAT = x => x.toFixed(2);
17+
const DEFAULT_TICK_FORMAT = (x: number) => x.toFixed(2);
1818
const DEFAULT_COLOR: Color = [0, 0, 0, 255];
1919

2020
interface LabelTexture {
@@ -33,9 +33,9 @@ const defaultProps: DefaultProps<AxesLayerProps> = {
3333
xTicks: DEFAULT_TICK_COUNT,
3434
yTicks: DEFAULT_TICK_COUNT,
3535
zTicks: DEFAULT_TICK_COUNT,
36-
xTickFormat: DEFAULT_TICK_FORMAT,
37-
yTickFormat: DEFAULT_TICK_FORMAT,
38-
zTickFormat: DEFAULT_TICK_FORMAT,
36+
xTickFormat: DEFAULT_TICK_FORMAT as TickFormat<unknown>,
37+
yTickFormat: DEFAULT_TICK_FORMAT as TickFormat<unknown>,
38+
zTickFormat: DEFAULT_TICK_FORMAT as TickFormat<unknown>,
3939
padding: 0,
4040
color: DEFAULT_COLOR,
4141
xTitle: 'x',

examples/website/plot/plot-layer/plot-layer.ts

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import {
2+
Accessor,
3+
AccessorContext,
4+
AccessorFunction,
25
Color,
36
CompositeLayer,
47
CompositeLayerProps,
@@ -9,23 +12,23 @@ import {ScaleLinear, scaleLinear} from 'd3-scale';
912

1013
import AxesLayer from './axes-layer';
1114
import SurfaceLayer from './surface-layer';
12-
import {$TODO} from './types';
15+
import {Range, TickFormat, Vec3} from './types';
1316

14-
const DEFAULT_GET_SCALE = {type: 'function', value: () => scaleLinear()};
15-
const DEFAULT_TICK_FORMAT = {type: 'function', value: x => x.toFixed(2)};
17+
const DEFAULT_GET_SCALE = {type: 'function', value: () => scaleLinear()} as const;
18+
const DEFAULT_TICK_FORMAT = {type: 'function', value: (x: number) => x.toFixed(2)} as const;
1619
const DEFAULT_TICK_COUNT = 6;
17-
const DEFAULT_COLOR = [0, 0, 0, 255];
20+
const DEFAULT_COLOR: Color = [0, 0, 0, 255];
1821

1922
/** All props supported by PlotLayer. */
20-
export type PlotLayerProps<DataT = unknown> = _PlotLayerProps<DataT> & CompositeLayerProps;
23+
export type PlotLayerProps<DataT extends Vec3 = Vec3> = _PlotLayerProps<DataT> & CompositeLayerProps;
2124

22-
type _PlotLayerProps<DataT> = {
25+
type _PlotLayerProps<DataT extends Vec3 = Vec3> = {
2326
// SurfaceLayer props
24-
getPosition: $TODO;
25-
getColor: $TODO;
26-
getXScale: $TODO;
27-
getYScale: $TODO;
28-
getZScale: $TODO;
27+
getPosition: AccessorFunction<DataT, Vec3>;
28+
getColor: AccessorFunction<DataT, Color>;
29+
getXScale: AccessorFunction<Range, ScaleLinear<number, number>>;
30+
getYScale: AccessorFunction<Range, ScaleLinear<number, number>>;
31+
getZScale: AccessorFunction<Range, ScaleLinear<number, number>>;
2932
uCount: number;
3033
vCount: number;
3134
lightStrength: number;
@@ -39,9 +42,9 @@ type _PlotLayerProps<DataT> = {
3942
xTicks: number;
4043
yTicks: number;
4144
zTicks: number;
42-
xTickFormat: $TODO;
43-
yTickFormat: $TODO;
44-
zTickFormat: $TODO;
45+
xTickFormat: TickFormat<DataT>;
46+
yTickFormat: TickFormat<DataT>;
47+
zTickFormat: TickFormat<DataT>;
4548
axesPadding: 0;
4649
axesColor: Color;
4750
xTitle: string;
@@ -51,8 +54,8 @@ type _PlotLayerProps<DataT> = {
5154

5255
const defaultProps: DefaultProps<PlotLayerProps> = {
5356
// SurfaceLayer props
54-
getPosition: {type: 'accessor', value: (u, v) => [0, 0, 0]},
55-
getColor: {type: 'accessor', value: (x, y, z) => DEFAULT_COLOR},
57+
getPosition: {type: 'accessor', value: ([u, v]) => [0, 0, 0]},
58+
getColor: {type: 'accessor', value: ([x, y, z]) => DEFAULT_COLOR},
5659
getXScale: DEFAULT_GET_SCALE,
5760
getYScale: DEFAULT_GET_SCALE,
5861
getZScale: DEFAULT_GET_SCALE,
@@ -66,9 +69,9 @@ const defaultProps: DefaultProps<PlotLayerProps> = {
6669
xTicks: DEFAULT_TICK_COUNT,
6770
yTicks: DEFAULT_TICK_COUNT,
6871
zTicks: DEFAULT_TICK_COUNT,
69-
xTickFormat: DEFAULT_TICK_FORMAT,
70-
yTickFormat: DEFAULT_TICK_FORMAT,
71-
zTickFormat: DEFAULT_TICK_FORMAT,
72+
xTickFormat: DEFAULT_TICK_FORMAT as unknown as TickFormat<unknown>,
73+
yTickFormat: DEFAULT_TICK_FORMAT as unknown as TickFormat<unknown>,
74+
zTickFormat: DEFAULT_TICK_FORMAT as unknown as TickFormat<unknown>,
7275
xTitle: 'x',
7376
yTitle: 'y',
7477
zTitle: 'z',
@@ -109,7 +112,7 @@ const defaultProps: DefaultProps<PlotLayerProps> = {
109112
* @param {Number} [props.fontSize] - size of the labels
110113
* @param {Array} [props.axesColor] - color of the gridlines, in [r,g,b,a]
111114
*/
112-
export default class PlotLayer<DataT = any, ExtraPropsT extends {} = {}> extends CompositeLayer<
115+
export default class PlotLayer<DataT extends Vec3 = Vec3, ExtraPropsT extends {} = {}> extends CompositeLayer<
113116
ExtraPropsT & Required<_PlotLayerProps<DataT>>
114117
> {
115118
static layerName = 'PlotLayer';
@@ -137,7 +140,7 @@ export default class PlotLayer<DataT = any, ExtraPropsT extends {} = {}> extends
137140
for (let uIndex = 0; uIndex < uCount; uIndex++) {
138141
const u = uIndex / (uCount - 1);
139142
const v = vIndex / (vCount - 1);
140-
const [x, y, z] = getPosition(u, v);
143+
const [x, y, z] = getPosition([u, v, 0] as DataT, {} as AccessorContext<DataT>);
141144

142145
if (isFinite(x)) {
143146
xMin = Math.min(xMin, x);
@@ -154,9 +157,9 @@ export default class PlotLayer<DataT = any, ExtraPropsT extends {} = {}> extends
154157
}
155158
}
156159

157-
const xScale = getXScale({min: xMin, max: xMax});
158-
const yScale = getYScale({min: yMin, max: yMax});
159-
const zScale = getZScale({min: zMin, max: zMax});
160+
const xScale = getXScale({min: xMin, max: xMax}, {} as AccessorContext<Range>);
161+
const yScale = getYScale({min: yMin, max: yMax}, {} as AccessorContext<Range>);
162+
const zScale = getZScale({min: zMin, max: zMax}, {} as AccessorContext<Range>);
160163

161164
this.setState({xScale, yScale, zScale});
162165
}
@@ -188,8 +191,8 @@ export default class PlotLayer<DataT = any, ExtraPropsT extends {} = {}> extends
188191
return [
189192
new SurfaceLayer(
190193
{
191-
getPosition,
192-
getColor,
194+
getPosition: getPosition as AccessorFunction<Vec3, Vec3>,
195+
getColor: getColor as Accessor<Vec3, Color>,
193196
uCount,
194197
vCount,
195198
xScale,

examples/website/plot/plot-layer/surface-layer.ts

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
import {Layer, picking} from '@deck.gl/core';
2-
import type {Color, DefaultProps, LayerDataSource, LayerProps} from '@deck.gl/core';
2+
import type {
3+
Accessor,
4+
AccessorContext,
5+
AccessorFunction,
6+
Color,
7+
DefaultProps,
8+
LayerDataSource,
9+
LayerProps
10+
} from '@deck.gl/core';
311
import {GL} from '@luma.gl/constants';
412
import {Model} from '@luma.gl/engine';
513
import {ScaleLinear} from 'd3-scale';
614

715
import surfaceVertex from './surface-vertex.glsl';
816
import fragmentShader from './fragment.glsl';
9-
import {$TODO} from './types';
17+
import {Vec3} from './types';
1018

1119
const DEFAULT_COLOR: Color = [0, 0, 0, 255];
1220

@@ -23,12 +31,12 @@ const defaultProps: DefaultProps<SurfaceLayerProps> = {
2331
};
2432

2533
/** All props supported by SurfaceLayer. */
26-
export type SurfaceLayerProps<DataT = unknown> = _SurfaceLayerProps<DataT> & LayerProps;
34+
export type SurfaceLayerProps<DataT extends Vec3 = Vec3> = _SurfaceLayerProps<DataT> & LayerProps;
2735

28-
type _SurfaceLayerProps<DataT> = {
36+
type _SurfaceLayerProps<DataT extends Vec3 = Vec3> = {
2937
data: LayerDataSource<DataT>;
30-
getPosition?: $TODO;
31-
getColor?: $TODO;
38+
getPosition?: AccessorFunction<DataT, Vec3>;
39+
getColor?: Accessor<DataT, Color>;
3240
xScale?: ScaleLinear<number, number>;
3341
yScale?: ScaleLinear<number, number>;
3442
zScale?: ScaleLinear<number, number>;
@@ -53,9 +61,10 @@ type _SurfaceLayerProps<DataT> = {
5361
* @param {Integer} [props.vCount] - number of samples within y range
5462
* @param {Number} [props.lightStrength] - front light strength
5563
*/
56-
export default class SurfaceLayer<DataT = any, ExtraPropsT extends {} = {}> extends Layer<
57-
ExtraPropsT & Required<_SurfaceLayerProps<DataT>>
58-
> {
64+
export default class SurfaceLayer<
65+
DataT extends Vec3 = Vec3,
66+
ExtraPropsT extends {} = {}
67+
> extends Layer<ExtraPropsT & Required<_SurfaceLayerProps<DataT>>> {
5968
static defaultProps = defaultProps;
6069
static layerName: string = 'SurfaceLayer';
6170

@@ -139,7 +148,7 @@ export default class SurfaceLayer<DataT = any, ExtraPropsT extends {} = {}> exte
139148
if (b === 0) {
140149
return -1;
141150
}
142-
return r | g << 8;
151+
return r | (g << 8);
143152
}
144153

145154
getPickingInfo(opts) {
@@ -149,7 +158,7 @@ export default class SurfaceLayer<DataT = any, ExtraPropsT extends {} = {}> exte
149158
const {getPosition} = this.props;
150159
const u = (info.index & 255) / 255;
151160
const v = (info.index >> 8) / 255;
152-
info.sample = getPosition(u, v);
161+
info.sample = getPosition([u, v, 0] as DataT, {} as AccessorContext<DataT>);
153162
}
154163

155164
return info;
@@ -205,7 +214,7 @@ export default class SurfaceLayer<DataT = any, ExtraPropsT extends {} = {}> exte
205214
for (let uIndex = 0; uIndex < uCount; uIndex++) {
206215
const u = uIndex / (uCount - 1);
207216
const v = vIndex / (vCount - 1);
208-
const [x, y, z] = getPosition(u, v);
217+
const [x, y, z] = getPosition([u, v, 0] as DataT, {} as AccessorContext<DataT>);
209218

210219
const isXFinite = isFinite(x);
211220
const isYFinite = isFinite(y);
@@ -231,12 +240,13 @@ export default class SurfaceLayer<DataT = any, ExtraPropsT extends {} = {}> exte
231240
const value = new Uint8ClampedArray(vertexCount! * attribute.size);
232241

233242
// Support constant colors
234-
let {getColor} = this.props;
235-
getColor = typeof getColor === 'function' ? getColor : () => getColor;
243+
const getColor =
244+
typeof this.props.getColor === 'function' ? this.props.getColor : () => this.props.getColor;
236245

237246
for (let i = 0; i < vertexCount; i++) {
238247
const index = i * 4;
239-
const color = getColor(positions[index], positions[index + 2], positions[index + 1]);
248+
const position = [positions[index], positions[index + 2], positions[index + 1]];
249+
const color = getColor(position as DataT, {} as AccessorContext<DataT>);
240250
value[i * 4] = color[0];
241251
value[i * 4 + 1] = color[1];
242252
value[i * 4 + 2] = color[2];
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
export type Vec2 = [number, number];
22
export type Vec3 = [number, number, number];
33

4+
export type Range = {min: number, max: number};
5+
46
export type Axis = 'x' | 'y' | 'z';
57

68
export type Tick = {
@@ -10,9 +12,3 @@ export type Tick = {
1012
};
1113

1214
export type TickFormat<DataT = unknown> = (t: DataT, axis: Axis) => string;
13-
14-
/**
15-
* Unresolved complaint from type checker. Used while migrating code to
16-
* TypeScript, and should be fixed when possible.
17-
*/
18-
export type $TODO = any;

0 commit comments

Comments
 (0)