Skip to content

Commit 97e17a1

Browse files
committed
Fix types for yaml editor
1 parent 4cfb54a commit 97e17a1

File tree

4 files changed

+182
-16
lines changed

4 files changed

+182
-16
lines changed

src/filters/filters.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
import filters, { FilterInput } from "./filters";
22

33
const RIGHT_1 = { integrate: { offset: "2d" } } satisfies FilterInput;
4+
const RIGHT_11 = { integrate: "d" } satisfies FilterInput;
45
const RIGHT_2 = "integrate" satisfies FilterInput;
56
const RIGHT_3 = "delta" satisfies FilterInput;
67
const RIGHT_4 = "deduplicate_adjacent" satisfies FilterInput;
8+
const RIGHT_5 = "force_numeric" satisfies FilterInput;
9+
const RIGHT_6 = "resample" satisfies FilterInput;
10+
const RIGHT_7 = { resample: "5m" } satisfies FilterInput;
11+
712
//@ts-expect-error
813
const WRONG_1 = "add" satisfies FilterInput;
14+
//@ts-expect-error
15+
const WRONG_2 = { integrate: 3 } satisfies FilterInput;
916

1017
const data = {
1118
states: [],

src/filters/filters.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,16 @@ type FilterParam<K extends keyof typeof filters> = Parameters<
3535
(typeof filters)[K]
3636
>[0];
3737

38+
type CheckType<T, IfUndef, IfNonUndef> =
39+
| (T extends undefined ? IfUndef : never)
40+
| (T extends unknown ? IfNonUndef : never);
41+
3842
export type FilterInput = {
39-
[K in keyof typeof filters]: FilterParam<K> extends undefined
40-
? // Case 1: Function accepts `undefined` only, return the key as a string literal
41-
K
42-
: undefined extends FilterParam<K>
43-
? object extends FilterParam<K>
44-
? // Case 2: Function accepts `object | undefined`, return union of the key or the object
45-
{ [P in K]: Exclude<FilterParam<K>, undefined> } | K
46-
: // Case 3: Function accepts only `undefined`, return the key as a string literal
47-
K
48-
: // Case 4: Function accepts only an `object`, return object type
49-
{ [P in K]: FilterParam<K> };
43+
[K in keyof typeof filters]: CheckType<
44+
FilterParam<K>,
45+
K,
46+
{ [P in K]: Exclude<FilterParam<K>, undefined> }
47+
>;
5048
}[keyof typeof filters];
5149

5250
const mapNumbers = (ys: YValue[], fn: (y: number, i: number) => number) =>

yaml-editor/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { type JSONSchemaForSchemaStoreOrgCatalogFiles } from "@schemastore/schema-catalog";
21
import {
32
editor,
43
languages,

yaml-editor/src/schema.json

Lines changed: 166 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1929,7 +1929,7 @@
19291929
},
19301930
"With$fn<Function>": {
19311931
"properties": {
1932-
"__@hasInstance@4208": {
1932+
"__@hasInstance@4211": {
19331933
"anyOf": [
19341934
{
19351935
"pattern": "^[\\s]*\\$(ex|fn)\\s[\\s\\S]+$",
@@ -2045,7 +2045,7 @@
20452045
}
20462046
},
20472047
"required": [
2048-
"__@hasInstance@4208",
2048+
"__@hasInstance@4211",
20492049
"apply",
20502050
"arguments",
20512051
"bind",
@@ -2060,7 +2060,7 @@
20602060
},
20612061
"With$fn<Function>_1": {
20622062
"properties": {
2063-
"__@hasInstance@4208": {
2063+
"__@hasInstance@4211": {
20642064
"anyOf": [
20652065
{
20662066
"pattern": "^[\\s]*\\$(ex|fn)\\s[\\s\\S]+$",
@@ -2176,7 +2176,7 @@
21762176
}
21772177
},
21782178
"required": [
2179-
"__@hasInstance@4208",
2179+
"__@hasInstance@4211",
21802180
"apply",
21812181
"arguments",
21822182
"bind",
@@ -73179,6 +73179,79 @@
7317973179
],
7318073180
"type": "object"
7318173181
},
73182+
"With$fn<{deduplicate_adjacent:never;}>": {
73183+
"properties": {
73184+
"deduplicate_adjacent": {
73185+
"anyOf": [
73186+
{
73187+
"pattern": "^[\\s]*\\$(ex|fn)\\s[\\s\\S]+$",
73188+
"type": "string"
73189+
},
73190+
{
73191+
"items": {
73192+
"$ref": "#/definitions/With$fn<unknown>"
73193+
},
73194+
"type": "array"
73195+
}
73196+
]
73197+
}
73198+
},
73199+
"required": [
73200+
"deduplicate_adjacent"
73201+
],
73202+
"type": "object"
73203+
},
73204+
"With$fn<{delta:never;}>": {
73205+
"properties": {
73206+
"delta": {
73207+
"anyOf": [
73208+
{
73209+
"pattern": "^[\\s]*\\$(ex|fn)\\s[\\s\\S]+$",
73210+
"type": "string"
73211+
},
73212+
{
73213+
"items": {
73214+
"$ref": "#/definitions/With$fn<unknown>"
73215+
},
73216+
"type": "array"
73217+
}
73218+
]
73219+
}
73220+
},
73221+
"required": [
73222+
"delta"
73223+
],
73224+
"type": "object"
73225+
},
73226+
"With$fn<{derivate:\"ms\"|\"s\"|\"m\"|\"h\"|\"d\"|\"w\"|\"M\"|\"y\";}>": {
73227+
"properties": {
73228+
"derivate": {
73229+
"anyOf": [
73230+
{
73231+
"pattern": "^[\\s]*\\$(ex|fn)\\s[\\s\\S]+$",
73232+
"type": "string"
73233+
},
73234+
{
73235+
"enum": [
73236+
"M",
73237+
"d",
73238+
"h",
73239+
"m",
73240+
"ms",
73241+
"s",
73242+
"w",
73243+
"y"
73244+
],
73245+
"type": "string"
73246+
}
73247+
]
73248+
}
73249+
},
73250+
"required": [
73251+
"derivate"
73252+
],
73253+
"type": "object"
73254+
},
7318273255
"With$fn<{entity?:Partial<Plotly.PlotData>|undefined;yaxes?:Partial<Partial<Plotly.LayoutAxis>>|undefined;}>": {
7318373256
"properties": {
7318473257
"entity": {
@@ -73497,6 +73570,9 @@
7349773570
{
7349873571
"$ref": "#/definitions/With$fn<{filter:string;}>"
7349973572
},
73573+
{
73574+
"$ref": "#/definitions/With$fn<{force_numeric:never;}>"
73575+
},
7350073576
{
7350173577
"$ref": "#/definitions/With$fn<{add:number;}>"
7350273578
},
@@ -73506,6 +73582,15 @@
7350673582
{
7350773583
"$ref": "#/definitions/With$fn<{calibrate_linear:`${number}->${number}`[];}>"
7350873584
},
73585+
{
73586+
"$ref": "#/definitions/With$fn<{deduplicate_adjacent:never;}>"
73587+
},
73588+
{
73589+
"$ref": "#/definitions/With$fn<{delta:never;}>"
73590+
},
73591+
{
73592+
"$ref": "#/definitions/With$fn<{derivate:\"ms\"|\"s\"|\"m\"|\"h\"|\"d\"|\"w\"|\"M\"|\"y\";}>"
73593+
},
7350973594
{
7351073595
"$ref": "#/definitions/With$fn<{integrate:\"ms\"|\"s\"|\"m\"|\"h\"|\"d\"|\"w\"|\"M\"|\"y\"|{unit?:\"ms\"|\"s\"|\"m\"|\"h\"|\"d\"|\"w\"|\"M\"|\"y\"|undefined;reset_every?:TimeDurationStr|undefined;offset?:TimeDurationStr|undefined;};}>"
7351173596
},
@@ -73527,6 +73612,9 @@
7352773612
{
7352873613
"$ref": "#/definitions/With$fn<{map_x:string;}>"
7352973614
},
73615+
{
73616+
"$ref": "#/definitions/With$fn<{resample:TimeDurationStr;}>"
73617+
},
7353073618
{
7353173619
"$ref": "#/definitions/With$fn<{load_var:string;}>"
7353273620
},
@@ -98811,6 +98899,28 @@
9881198899
],
9881298900
"type": "object"
9881398901
},
98902+
"With$fn<{force_numeric:never;}>": {
98903+
"properties": {
98904+
"force_numeric": {
98905+
"anyOf": [
98906+
{
98907+
"pattern": "^[\\s]*\\$(ex|fn)\\s[\\s\\S]+$",
98908+
"type": "string"
98909+
},
98910+
{
98911+
"items": {
98912+
"$ref": "#/definitions/With$fn<unknown>"
98913+
},
98914+
"type": "array"
98915+
}
98916+
]
98917+
}
98918+
},
98919+
"required": [
98920+
"force_numeric"
98921+
],
98922+
"type": "object"
98923+
},
9881498924
"With$fn<{integrate:\"ms\"|\"s\"|\"m\"|\"h\"|\"d\"|\"w\"|\"M\"|\"y\"|{unit?:\"ms\"|\"s\"|\"m\"|\"h\"|\"d\"|\"w\"|\"M\"|\"y\"|undefined;reset_every?:TimeDurationStr|undefined;offset?:TimeDurationStr|undefined;};}>": {
9881598925
"properties": {
9881698926
"integrate": {
@@ -98981,6 +99091,58 @@
9898199091
],
9898299092
"type": "object"
9898399093
},
99094+
"With$fn<{resample:TimeDurationStr;}>": {
99095+
"properties": {
99096+
"resample": {
99097+
"anyOf": [
99098+
{
99099+
"pattern": "^[0-9]*ms$",
99100+
"type": "string"
99101+
},
99102+
{
99103+
"pattern": "^[0-9]*s$",
99104+
"type": "string"
99105+
},
99106+
{
99107+
"pattern": "^[0-9]*m$",
99108+
"type": "string"
99109+
},
99110+
{
99111+
"pattern": "^[0-9]*h$",
99112+
"type": "string"
99113+
},
99114+
{
99115+
"pattern": "^[0-9]*d$",
99116+
"type": "string"
99117+
},
99118+
{
99119+
"pattern": "^[0-9]*w$",
99120+
"type": "string"
99121+
},
99122+
{
99123+
"pattern": "^[0-9]*M$",
99124+
"type": "string"
99125+
},
99126+
{
99127+
"pattern": "^[0-9]*y$",
99128+
"type": "string"
99129+
},
99130+
{
99131+
"pattern": "^[\\s]*\\$(ex|fn)\\s[\\s\\S]+$",
99132+
"type": "string"
99133+
},
99134+
{
99135+
"const": "0",
99136+
"type": "string"
99137+
}
99138+
]
99139+
}
99140+
},
99141+
"required": [
99142+
"resample"
99143+
],
99144+
"type": "object"
99145+
},
9898499146
"With$fn<{right_margin:number;}>": {
9898599147
"properties": {
9898699148
"right_margin": {

0 commit comments

Comments
 (0)