Skip to content

Commit

Permalink
fix(types): use StaticDecode vs Static to infer types (#797)
Browse files Browse the repository at this point in the history
With Typebox, `Static` is used to infer types from schema, however,
`StaticDecode` is needed for Transform types which have a `Decode()`
function.  This is important also for schemas composed of other
schemas that make use of transform types.

We already use `StaticDecode` in `validateAndCoerceTypes()` which
is the most important place and filters down to regular use of the
library.  But now that we are exporting all types again (per previous
commit), this is important to make sure that users who are importing
types directly get the correct type.  And I think solved some edge
cases where certain types will still coming through incorrectly.

cc: @eddie-atkinson
  • Loading branch information
gadicc committed Sep 18, 2024
1 parent 5ca349e commit 696baf6
Show file tree
Hide file tree
Showing 13 changed files with 152 additions and 122 deletions.
12 changes: 6 additions & 6 deletions src/modules/chart.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Co-authored by @gadicc, @PythonCreator27 and @huned.

import { Static, Type } from "@sinclair/typebox";
import { StaticDecode, Type } from "@sinclair/typebox";
import type {
ModuleOptions,
ModuleOptionsWithValidateTrue,
Expand Down Expand Up @@ -262,15 +262,15 @@ const ChartOptionsWithReturnObjectSchema = Type.Composite(
},
);

export type ChartOptions = Static<typeof ChartOptionsSchema>;
export type ChartOptionsWithReturnObject = Static<
export type ChartOptions = StaticDecode<typeof ChartOptionsSchema>;
export type ChartOptionsWithReturnObject = StaticDecode<
typeof ChartOptionsWithReturnObjectSchema
>;
export type ChartResultObject = Static<typeof ChartResultObjectSchema>;
export type ChartOptionsWithReturnArray = Static<
export type ChartResultObject = StaticDecode<typeof ChartResultObjectSchema>;
export type ChartOptionsWithReturnArray = StaticDecode<
typeof ChartOptionsWithReturnArraySchema
>;
export type ChartResultArray = Static<typeof ChartResultArraySchema>;
export type ChartResultArray = StaticDecode<typeof ChartResultArraySchema>;

const queryOptionsDefaults: Omit<ChartOptions, "period1"> = {
useYfid: true,
Expand Down
8 changes: 5 additions & 3 deletions src/modules/dailyGainers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Static, Type } from "@sinclair/typebox";
import { StaticDecode, Type } from "@sinclair/typebox";
import type {
ModuleOptions,
ModuleOptionsWithValidateTrue,
Expand Down Expand Up @@ -155,8 +155,10 @@ const DailyGainersResultSchema = Type.Object(
{ title: "DailyGainersResult" },
);

export type DailyGainersResult = Static<typeof DailyGainersResultSchema>;
export type DailyGainersOptions = Static<typeof DailyGainersOptionsSchema>;
export type DailyGainersResult = StaticDecode<typeof DailyGainersResultSchema>;
export type DailyGainersOptions = StaticDecode<
typeof DailyGainersOptionsSchema
>;

const queryOptionsDefaults = {
lang: "en-US",
Expand Down
6 changes: 3 additions & 3 deletions src/modules/fundamentalsTimeSeries.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Static, Type } from "@sinclair/typebox";
import { StaticDecode, Type } from "@sinclair/typebox";
import type {
ModuleOptions,
ModuleOptionsWithValidateTrue,
Expand Down Expand Up @@ -47,15 +47,15 @@ const FundamentalsTimeSeriesOptionsSchema = Type.Object(
},
);

export type FundamentalsTimeSeriesOptions = Static<
export type FundamentalsTimeSeriesOptions = StaticDecode<
typeof FundamentalsTimeSeriesOptionsSchema
>;

const FundamentalsTimeSeriesResultsSchema = Type.Array(
FundamentalsTimeSeriesResultSchema,
);

export type FundamentalsTimeSeriesResult = Static<
export type FundamentalsTimeSeriesResult = StaticDecode<
typeof FundamentalsTimeSeriesResultSchema
>;

Expand Down
26 changes: 15 additions & 11 deletions src/modules/historical.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Static, Type } from "@sinclair/typebox";
import { StaticDecode, Type } from "@sinclair/typebox";
import { Value } from "@sinclair/typebox/value";
import type {
ModuleOptions,
Expand Down Expand Up @@ -26,7 +26,9 @@ const HistoricalRowHistorySchema = Type.Object(
title: "HistoricalRowHistory",
},
);
export type HistoricalRowHistory = Static<typeof HistoricalRowHistorySchema>;
export type HistoricalRowHistory = StaticDecode<
typeof HistoricalRowHistorySchema
>;

const HistoricalRowDividendSchema = Type.Object(
{
Expand All @@ -35,7 +37,9 @@ const HistoricalRowDividendSchema = Type.Object(
},
{ title: "HistoricalRowDividend" },
);
export type HistoricalRowDividend = Static<typeof HistoricalRowDividendSchema>;
export type HistoricalRowDividend = StaticDecode<
typeof HistoricalRowDividendSchema
>;

const HistoricalRowStockSplitSchema = Type.Object(
{
Expand All @@ -44,7 +48,7 @@ const HistoricalRowStockSplitSchema = Type.Object(
},
{ title: "HistoricalRowStockSplit" },
);
export type HistoricalRowStockSplit = Static<
export type HistoricalRowStockSplit = StaticDecode<
typeof HistoricalRowStockSplitSchema
>;

Expand Down Expand Up @@ -73,7 +77,7 @@ const HistoricalOptionsSchema = Type.Object(
},
{ title: "HistoricalOptions" },
);
export type HistoricalOptions = Static<typeof HistoricalOptionsSchema>;
export type HistoricalOptions = StaticDecode<typeof HistoricalOptionsSchema>;

const HistoricalOptionsEventsHistorySchema = Type.Composite(
[
Expand All @@ -84,7 +88,7 @@ const HistoricalOptionsEventsHistorySchema = Type.Composite(
],
{ title: "HistoricalOptionsEventsHistory" },
);
export type HistoricalOptionsEventsHistory = Static<
export type HistoricalOptionsEventsHistory = StaticDecode<
typeof HistoricalOptionsEventsHistorySchema
>;

Expand All @@ -97,7 +101,7 @@ const HistoricalOptionsEventsDividendsSchema = Type.Composite(
],
{ title: "HistoricalOptionsEventsDividends" },
);
export type HistoricalOptionsEventsDividends = Static<
export type HistoricalOptionsEventsDividends = StaticDecode<
typeof HistoricalOptionsEventsDividendsSchema
>;

Expand All @@ -110,14 +114,14 @@ const HistoricalOptionsEventsSplitSchema = Type.Composite(
],
{ title: "HistoricalOptionsEventsSplit" },
);
export type HistoricalOptionsEventsSplit = Static<
export type HistoricalOptionsEventsSplit = StaticDecode<
typeof HistoricalOptionsEventsSplitSchema
>;

const HistoricalHistoryResultSchema = Type.Array(HistoricalRowHistorySchema, {
title: "HistoricalHistoryResult",
});
export type HistoricalHistoryResult = Static<
export type HistoricalHistoryResult = StaticDecode<
typeof HistoricalHistoryResultSchema
>;

Expand All @@ -127,7 +131,7 @@ const HistoricalDividendsResultSchema = Type.Array(
title: "HistoricalDividendsResult",
},
);
export type HistoricalDividendsResult = Static<
export type HistoricalDividendsResult = StaticDecode<
typeof HistoricalDividendsResultSchema
>;

Expand All @@ -137,7 +141,7 @@ const HistoricalStockSplitsResultSchema = Type.Array(
title: "HistoricalRowStockSplit",
},
);
export type HistoricalStockSplitsResult = Static<
export type HistoricalStockSplitsResult = StaticDecode<
typeof HistoricalStockSplitsResultSchema
>;

Expand Down
8 changes: 4 additions & 4 deletions src/modules/insights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {
ModuleOptionsWithValidateFalse,
ModuleThis,
} from "../lib/moduleCommon.js";
import { Type, Static } from "@sinclair/typebox";
import { Type, StaticDecode } from "@sinclair/typebox";

import {
YahooDateInMs,
Expand Down Expand Up @@ -236,8 +236,8 @@ const InsightsResultSchema = Type.Object(
},
);

export type InsightsResult = Static<typeof InsightsResultSchema>;
export type InsightsOutlook = Static<typeof InsightsOutlookSchema>;
export type InsightsResult = StaticDecode<typeof InsightsResultSchema>;
export type InsightsOutlook = StaticDecode<typeof InsightsOutlookSchema>;

export interface InsightsInstrumentInfo {
[key: string]: any;
Expand Down Expand Up @@ -298,7 +298,7 @@ const InsightsOptionsSchema = Type.Object(
{ title: "InsightsOptions" },
);

export type InsightsOptions = Static<typeof InsightsOptionsSchema>;
export type InsightsOptions = StaticDecode<typeof InsightsOptionsSchema>;

const queryOptionsDefaults = {
lang: "en-US",
Expand Down
6 changes: 3 additions & 3 deletions src/modules/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {
ModuleOptionsWithValidateFalse,
ModuleThis,
} from "../lib/moduleCommon.js";
import { Type, Static } from "@sinclair/typebox";
import { Type, StaticDecode } from "@sinclair/typebox";

import { YahooFinanceDate, YahooNumber } from "../lib/yahooFinanceTypes.js";
import { QuoteBase } from "./quote.js";
Expand Down Expand Up @@ -212,8 +212,8 @@ const OptionsOptionsSchema = Type.Object(
},
);

export type OptionsOptions = Static<typeof OptionsOptionsSchema>;
export type OptionsResult = Static<typeof OptionsResultSchema>;
export type OptionsOptions = StaticDecode<typeof OptionsOptionsSchema>;
export type OptionsResult = StaticDecode<typeof OptionsResultSchema>;

const queryOptionsDefaults: OptionsOptions = {
formatted: false,
Expand Down
12 changes: 6 additions & 6 deletions src/modules/quote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
ModuleThis,
} from "../lib/moduleCommon.js";

import { Static, Type } from "@sinclair/typebox";
import { StaticDecode, Type } from "@sinclair/typebox";
import {
YahooDateInMs,
YahooFinanceDate,
Expand Down Expand Up @@ -229,7 +229,7 @@ const QuoteSchema = Type.Union([
QuoteOption,
]);

export type Quote = Static<typeof QuoteSchema>;
export type Quote = StaticDecode<typeof QuoteSchema>;

const QuoteField = Type.KeyOf(QuoteSchema);

Expand Down Expand Up @@ -270,19 +270,19 @@ const QuoteOptionsWithReturnObjectSchema = Type.Composite([
}),
]);

export type QuoteOptionsWithReturnArray = Static<
export type QuoteOptionsWithReturnArray = StaticDecode<
typeof QuoteOptionsWithReturnArraySchema
>;

export type QuoteOptionsWithReturnMap = Static<
export type QuoteOptionsWithReturnMap = StaticDecode<
typeof QuoteOptionsWithReturnMapSchema
>;

export type QuoteOptionsWithReturnObject = Static<
export type QuoteOptionsWithReturnObject = StaticDecode<
typeof QuoteOptionsWithReturnObjectSchema
>;

export type QuoteOptions = Static<typeof QuoteOptionsSchema>;
export type QuoteOptions = StaticDecode<typeof QuoteOptionsSchema>;

const queryOptionsDefaults = {};

Expand Down
Loading

0 comments on commit 696baf6

Please sign in to comment.