Skip to content

Commit

Permalink
fix: Make params types more precise for UnitSpecs
Browse files Browse the repository at this point in the history
  • Loading branch information
arvind committed Jul 20, 2022
1 parent 34f925e commit 584dc64
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/normalize/index.ts
Expand Up @@ -48,7 +48,7 @@ const topLevelSelectionNormalizer = new TopLevelSelectionsNormalizer();
* And push top-level selection definitions down to unit specs.
*/
function normalizeGenericSpec(
spec: NonNormalizedSpec | FacetedUnitSpec<Field> | RepeatSpec,
spec: NonNormalizedSpec | FacetedUnitSpec<Field, any> | RepeatSpec,
config: Config<SignalRef> = {}
) {
const normParams = {config};
Expand Down
4 changes: 3 additions & 1 deletion src/spec/toplevel.ts
Expand Up @@ -16,6 +16,8 @@ export type Padding = number | {top?: number; bottom?: number; left?: number; ri

export type Datasets = Dict<InlineDataset>;

export type TopLevelParameter = VariableParameter | TopLevelSelectionParameter;

export type TopLevel<S extends BaseSpec> = S &
TopLevelProperties & {
/**
Expand Down Expand Up @@ -72,7 +74,7 @@ export interface TopLevelProperties<ES extends ExprRef | SignalRef = ExprRef | S
/**
* Dynamic variables or selections that parameterize a visualization.
*/
params?: (VariableParameter | TopLevelSelectionParameter)[];
params?: TopLevelParameter[];
}

export type FitType = 'fit' | 'fit-x' | 'fit-y';
Expand Down
15 changes: 9 additions & 6 deletions src/spec/unit.ts
Expand Up @@ -3,16 +3,15 @@ import {CompositeEncoding, FacetedCompositeEncoding} from '../compositemark';
import {Encoding} from '../encoding';
import {ExprRef} from '../expr';
import {AnyMark, Mark, MarkDef} from '../mark';
import {VariableParameter} from '../parameter';
import {Projection} from '../projection';
import {SelectionParameter} from '../selection';
import {Field} from './../channeldef';
import {BaseSpec, DataMixins, FrameMixins, GenericCompositionLayout, ResolveMixins} from './base';
import {TopLevel} from './toplevel';
import {TopLevel, TopLevelParameter} from './toplevel';
/**
* Base interface for a unit (single-view) specification.
*/
export interface GenericUnitSpec<E extends Encoding<any>, M> extends BaseSpec {
export interface GenericUnitSpec<E extends Encoding<any>, M, P = SelectionParameter> extends BaseSpec {
/**
* A string describing the mark type (one of `"bar"`, `"circle"`, `"square"`, `"tick"`, `"line"`,
* `"area"`, `"point"`, `"rule"`, `"geoshape"`, and `"text"`) or a [mark definition object](https://vega.github.io/vega-lite/docs/mark.html#mark-def).
Expand All @@ -33,7 +32,7 @@ export interface GenericUnitSpec<E extends Encoding<any>, M> extends BaseSpec {
/**
* An array of parameters that may either be simple variables, or more complex selections that map user input to data queries.
*/
params?: (VariableParameter | SelectionParameter)[];
params?: P[];
}

/**
Expand All @@ -51,12 +50,16 @@ export type UnitSpecWithFrame<F extends Field> = GenericUnitSpec<CompositeEncodi
/**
* Unit spec that can have a composite mark and row or column channels (shorthand for a facet spec).
*/
export type FacetedUnitSpec<F extends Field> = GenericUnitSpec<FacetedCompositeEncoding<F>, AnyMark> &
export type FacetedUnitSpec<F extends Field, P = SelectionParameter> = GenericUnitSpec<
FacetedCompositeEncoding<F>,
AnyMark,
P
> &
ResolveMixins &
GenericCompositionLayout &
FrameMixins;

export type TopLevelUnitSpec<F extends Field> = TopLevel<FacetedUnitSpec<F>> & DataMixins;
export type TopLevelUnitSpec<F extends Field> = TopLevel<FacetedUnitSpec<F, TopLevelParameter>> & DataMixins;

export function isUnitSpec(spec: BaseSpec): spec is FacetedUnitSpec<any> | NormalizedUnitSpec {
return 'mark' in spec;
Expand Down
2 changes: 1 addition & 1 deletion test/compile/selection/parse.test.ts
Expand Up @@ -306,7 +306,7 @@ describe('Selection', () => {
params: [
{
name: 'index',
value: {x: {year: 2005, month: 1, date: 1}},
value: [{x: {year: 2005, month: 1, date: 1}}],
select: {
type: 'point',
on: 'mouseover',
Expand Down
4 changes: 2 additions & 2 deletions test/compile/selection/scales.test.ts
Expand Up @@ -11,13 +11,13 @@ describe('Selection + Scales', () => {
describe('selectionExtent', () => {
it('is assembled from selection parameter', () => {
const model = parseConcatModel({
params: [{name: 'var'}],
vconcat: [
{
mark: 'area',
params: [
{name: 'brush', select: {type: 'interval', encodings: ['x']}},
{name: 'brush2', select: {type: 'point', fields: ['price'], resolve: 'intersect'}},
{name: 'var'}
{name: 'brush2', select: {type: 'point', fields: ['price'], resolve: 'intersect'}}
],
encoding: {
x: {field: 'date', type: 'temporal'},
Expand Down

0 comments on commit 584dc64

Please sign in to comment.