Skip to content

Commit 102187e

Browse files
committed
ts-pattern exhaustiveness check
1 parent 4c72a8a commit 102187e

File tree

3 files changed

+68
-49
lines changed

3 files changed

+68
-49
lines changed

app/components/CreateOrder.tsx

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { API, OrderEntity, OrderSide, OrderType } from '@orderly.network/types';
33
import { Separator } from '@radix-ui/themes';
44
import { FC, useState } from 'react';
55
import { Controller, FieldError, SubmitHandler, useForm } from 'react-hook-form';
6+
import { match } from 'ts-pattern';
67

78
import { Spinner, TokenInput } from '.';
89

@@ -29,8 +30,14 @@ export const CreateOrder: FC<{
2930
const { onSubmit, helper, maxQty, estLeverage, estLiqPrice } = useOrderEntry(
3031
{
3132
symbol: symbol.symbol,
32-
side: watch('direction', 'Buy') === 'Buy' ? OrderSide.BUY : OrderSide.SELL,
33-
order_type: watch('type', 'Market') === 'Market' ? OrderType.MARKET : OrderType.LIMIT,
33+
side: match(watch('direction', 'Buy'))
34+
.with('Buy', () => OrderSide.BUY)
35+
.with('Sell', () => OrderSide.SELL)
36+
.exhaustive(),
37+
order_type: match(watch('type', 'Market'))
38+
.with('Market', () => OrderType.MARKET)
39+
.with('Limit', () => OrderType.LIMIT)
40+
.exhaustive(),
3441
order_quantity: watch('quantity', undefined),
3542
order_price: watch('price', undefined)
3643
},
@@ -196,8 +203,14 @@ async function getValidationErrors(
196203
function getInput(data: Inputs, symbol: string): OrderEntity {
197204
return {
198205
symbol,
199-
side: data.direction === 'Buy' ? OrderSide.BUY : OrderSide.SELL,
200-
order_type: data.type === 'Market' ? OrderType.MARKET : OrderType.LIMIT,
206+
side: match(data.direction)
207+
.with('Buy', () => OrderSide.BUY)
208+
.with('Sell', () => OrderSide.SELL)
209+
.exhaustive(),
210+
order_type: match(data.type)
211+
.with('Market', () => OrderType.MARKET)
212+
.with('Limit', () => OrderType.LIMIT)
213+
.exhaustive(),
201214
order_price: data.price,
202215
order_quantity: data.quantity
203216
};

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
"typecheck": "tsc"
2424
},
2525
"dependencies": {
26-
"@orderly.network/hooks": "1.2.0",
27-
"@orderly.network/trading-view": "^1.2.0",
28-
"@orderly.network/types": "1.2.0",
26+
"@orderly.network/hooks": "1.3.0-alpha.0",
27+
"@orderly.network/trading-view": "1.3.0-alpha.0",
28+
"@orderly.network/types": "1.3.0-alpha.0",
2929
"@radix-ui/react-icons": "^1.3.0",
3030
"@radix-ui/themes": "^3.0.1",
3131
"@remix-run/cloudflare": "^2.8.1",
@@ -41,6 +41,7 @@
4141
"react": "^18.2.0",
4242
"react-dom": "^18.2.0",
4343
"react-hook-form": "^7.51.2",
44+
"ts-pattern": "^5.1.1",
4445
"use-resize-observer": "^9.1.0"
4546
},
4647
"devDependencies": {

yarn.lock

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,67 +1872,67 @@
18721872
dependencies:
18731873
which "^3.0.0"
18741874

1875-
"@orderly.network/core@1.2.0":
1876-
version "1.2.0"
1877-
resolved "https://registry.yarnpkg.com/@orderly.network/core/-/core-1.2.0.tgz#383bf795aae914fb35bac9b520d06375eabcceb4"
1878-
integrity sha512-vjKRdq7eV2P49mYbVlPfaQBFAmk6a0YcX5IQBfh1Zb96FL+cNRZst+8gAqKl3jZoT8b9JWwo8fr2wvE+OT3rUA==
1875+
"@orderly.network/core@1.3.0-alpha.0":
1876+
version "1.3.0-alpha.0"
1877+
resolved "https://registry.yarnpkg.com/@orderly.network/core/-/core-1.3.0-alpha.0.tgz#8a37fd895533e41eac32323bae9f2eefaea4215d"
1878+
integrity sha512-Zj6cfojIo9hAKVwuk+qpfw+tQ+ug3McYHn9Z6iscKFAqVdFHd5H6oeHe04xubgWulAgGh4Der7oCMCOo9O4O+w==
18791879
dependencies:
18801880
"@enzoferey/ethers-error-parser" "^0.2.3"
18811881
"@noble/ed25519" "^2.0.0"
1882-
"@orderly.network/types" "1.2.0"
1882+
"@orderly.network/types" "1.3.0-alpha.0"
18831883
bs58 "^5.0.0"
18841884
ethers "^6.7.1"
18851885
eventemitter3 "^5.0.1"
18861886
lodash.merge "^4.6.2"
18871887

1888-
"@orderly.network/hooks@1.2.0":
1889-
version "1.2.0"
1890-
resolved "https://registry.yarnpkg.com/@orderly.network/hooks/-/hooks-1.2.0.tgz#f76375dde32495bf9160b0b9319bdf16ec2a0d51"
1891-
integrity sha512-sUiQ/O1FhH575dMQ/WwEf6Vtc5k+FqeqBtkCj+O85vLdFLwVF1TqrCny1iSzYmqy4VJe+cidlujfm+XI23Y+cg==
1892-
dependencies:
1893-
"@orderly.network/core" "1.2.0"
1894-
"@orderly.network/net" "1.2.0"
1895-
"@orderly.network/perp" "3.0.0"
1896-
"@orderly.network/types" "1.2.0"
1897-
"@orderly.network/utils" "1.2.0"
1888+
"@orderly.network/hooks@1.3.0-alpha.0":
1889+
version "1.3.0-alpha.0"
1890+
resolved "https://registry.yarnpkg.com/@orderly.network/hooks/-/hooks-1.3.0-alpha.0.tgz#5fcb42a7860075f45ac35fb4da1e06c8b7bfb458"
1891+
integrity sha512-1tNmSAvtssH6N0rH9elSR9MweuO+5+StI0nsSPnGI6wnA8AOmdBc8vCbV53bAgAPoc9x/WJcul5nkZdWhg7J4A==
1892+
dependencies:
1893+
"@orderly.network/core" "1.3.0-alpha.0"
1894+
"@orderly.network/net" "1.3.0-alpha.0"
1895+
"@orderly.network/perp" "3.1.0-alpha.0"
1896+
"@orderly.network/types" "1.3.0-alpha.0"
1897+
"@orderly.network/utils" "1.3.0-alpha.0"
18981898
ramda "^0.29.0"
18991899
swr "^2.2.4"
19001900
use-constant "^1.1.1"
19011901
use-debounce "^9.0.4"
19021902

1903-
"@orderly.network/net@1.2.0":
1904-
version "1.2.0"
1905-
resolved "https://registry.yarnpkg.com/@orderly.network/net/-/net-1.2.0.tgz#d2372d0cb604ce936b58b348b3088b85681b5e43"
1906-
integrity sha512-ndFYuMaCGCjgZ/luZkZ22rQtliK7tWTJT1JVSxQFc+RNFf6IDkic4KgiKIra5Dk91L7aFYN0WqFFEsYpiG3+/Q==
1903+
"@orderly.network/net@1.3.0-alpha.0":
1904+
version "1.3.0-alpha.0"
1905+
resolved "https://registry.yarnpkg.com/@orderly.network/net/-/net-1.3.0-alpha.0.tgz#aee41665c2048a5dc83741292c03fb3d3457ee4b"
1906+
integrity sha512-8I8PnbpcXEB715uDVuqig6JkYJJRA8+HBFp4RMWwccy23PHBB/+ZDGHo6pJv0M7ecMbbBn1XfZGfS+Tj2LoWyQ==
19071907

1908-
"@orderly.network/perp@3.0.0":
1909-
version "3.0.0"
1910-
resolved "https://registry.yarnpkg.com/@orderly.network/perp/-/perp-3.0.0.tgz#501ed05da23a0c0a17b086e956aef145bdbc48d0"
1911-
integrity sha512-nM0X73zTrN4jDhUSDbdviqiSNgoWYWokwoSMRme3BpzDJg1gcjcf9EYx1ScWGwW1/xBtqK+z8nvujH8fx2qKjg==
1908+
"@orderly.network/perp@3.1.0-alpha.0":
1909+
version "3.1.0-alpha.0"
1910+
resolved "https://registry.yarnpkg.com/@orderly.network/perp/-/perp-3.1.0-alpha.0.tgz#eb3b3d5e0a16c1aa4d3f1056fbb07b6f984f483c"
1911+
integrity sha512-PO2qjWWmghvBDOVKatrLx2L0cWnqnO2Fsi0Gg0f1j02+FBINMGcO0g8fqNly12eFUXcHo46YM6xQlsmQygvkww==
19121912
dependencies:
1913-
"@orderly.network/types" "1.2.0"
1914-
"@orderly.network/utils" "1.2.0"
1913+
"@orderly.network/types" "1.3.0-alpha.0"
1914+
"@orderly.network/utils" "1.3.0-alpha.0"
19151915

1916-
"@orderly.network/trading-view@^1.2.0":
1917-
version "1.2.0"
1918-
resolved "https://registry.yarnpkg.com/@orderly.network/trading-view/-/trading-view-1.2.0.tgz#0325a005279129fcef6e3428174084ddab9c5177"
1919-
integrity sha512-n8eTt77I3lNXJVOeMJZ/+on48mzVaLkxy0lrb4hMo62HO/dvhutj00Pm9ewVe4ketgo5Gze9CpBjePM74zMGNg==
1916+
"@orderly.network/trading-view@1.3.0-alpha.0":
1917+
version "1.3.0-alpha.0"
1918+
resolved "https://registry.yarnpkg.com/@orderly.network/trading-view/-/trading-view-1.3.0-alpha.0.tgz#dee6e183af2284f16f35a1746babee43868aba47"
1919+
integrity sha512-YzbPdz5oDEcdmbJCFjmmv52jvFFNEwiZP2jEEJeWjoObb/BxDipvA7P0o5hpvQ80SX8eBPd/8sEZlTJ7dEoOMg==
19201920
dependencies:
1921-
"@orderly.network/core" "1.2.0"
1922-
"@orderly.network/hooks" "1.2.0"
1923-
"@orderly.network/net" "1.2.0"
1924-
"@orderly.network/types" "1.2.0"
1925-
"@orderly.network/utils" "1.2.0"
1921+
"@orderly.network/core" "1.3.0-alpha.0"
1922+
"@orderly.network/hooks" "1.3.0-alpha.0"
1923+
"@orderly.network/net" "1.3.0-alpha.0"
1924+
"@orderly.network/types" "1.3.0-alpha.0"
1925+
"@orderly.network/utils" "1.3.0-alpha.0"
19261926

1927-
"@orderly.network/types@1.2.0":
1928-
version "1.2.0"
1929-
resolved "https://registry.yarnpkg.com/@orderly.network/types/-/types-1.2.0.tgz#229671239e2d065e0d7c873e89f1608859ce5960"
1930-
integrity sha512-z7Imba7nZf2XH8WKFrXR3syEH9fVPvw7QSvFHRE19kNyYseQnwahhUySAzEHCOESYRPMWBUEGPYFlCaPxbAxuA==
1927+
"@orderly.network/types@1.3.0-alpha.0":
1928+
version "1.3.0-alpha.0"
1929+
resolved "https://registry.yarnpkg.com/@orderly.network/types/-/types-1.3.0-alpha.0.tgz#a81d6e3c9159aee88b9638a9b32dc91c3e33bff3"
1930+
integrity sha512-cnObFgu8uk86+ymk7XQDQiq4uYVZbKk6M0/9nfZpJBdULd1IIOp9ULNHCvf6QSTY3YcuF35OzR8Zdo6kZh6hWw==
19311931

1932-
"@orderly.network/utils@1.2.0":
1933-
version "1.2.0"
1934-
resolved "https://registry.yarnpkg.com/@orderly.network/utils/-/utils-1.2.0.tgz#e95937894dbf69db695fa7b8498fcd10bb1a0cf8"
1935-
integrity sha512-IICjd4ydcgbyudrzb2B/fImBVE8BD+kvIqzt//2NBe+i5C2DhkgPDbY5MQEJf+JNAypqXR+rMIJl5dGye2xBVQ==
1932+
"@orderly.network/utils@1.3.0-alpha.0":
1933+
version "1.3.0-alpha.0"
1934+
resolved "https://registry.yarnpkg.com/@orderly.network/utils/-/utils-1.3.0-alpha.0.tgz#946077b71965e45b0b47ef0e1dff7eb8c11c8d98"
1935+
integrity sha512-d05Mk76N6tNqYGwMNKZABOVOx70oiqLc7Q3MpAA/6zTXENHg2tX69uQOASVWbIWc8TnLWgnuoDtSV5rjq6laKw==
19361936
dependencies:
19371937
dayjs "^1.11.9"
19381938
decimal.js-light "^2.5.1"
@@ -9705,6 +9705,11 @@ ts-api-utils@^1.0.1:
97059705
resolved "https://registry.yarnpkg.com/ts-api-utils/-/ts-api-utils-1.3.0.tgz#4b490e27129f1e8e686b45cc4ab63714dc60eea1"
97069706
integrity sha512-UQMIo7pb8WRomKR1/+MFVLTroIvDVtMX3K6OUir8ynLyzB8Jeriont2bTAtmNPa1ekAgN7YPDyf6V+ygrdU+eQ==
97079707

9708+
ts-pattern@^5.1.1:
9709+
version "5.1.1"
9710+
resolved "https://registry.yarnpkg.com/ts-pattern/-/ts-pattern-5.1.1.tgz#600e0a3d23ab1b0eb30b7c897d0898e940504544"
9711+
integrity sha512-i+owkHr5RYdQxj8olUgRrqpiWH9x27PuWVfXwDmJ/n/CoF/SAa7WW1i2oUpPDMQpJ4U+bGRUcZkVq7i1m3zFCg==
9712+
97089713
tsconfck@^3.0.3:
97099714
version "3.0.3"
97109715
resolved "https://registry.yarnpkg.com/tsconfck/-/tsconfck-3.0.3.tgz#d9bda0e87d05b1c360e996c9050473c7e6f8084f"

0 commit comments

Comments
 (0)