Skip to content

Commit

Permalink
feat: now allow for array simple tokens like Array<string(one,two)> o…
Browse files Browse the repository at this point in the history
…r Array<number(1,2)
  • Loading branch information
yankeeinlondon committed Sep 10, 2024
1 parent 40eca8f commit 36accd4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/constants/TypeTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ export const SIMPLE_DICT_TOKENS = [
export const SIMPLE_ARRAY_TOKENS = [
"Array",
"Array<string>",
"Array<string(TOKEN)>" as `Array<string(${string})>`,
"Array<number>",
"Array<number(TOKEN)>" as `Array<number(${number}${`,${string}` | ""})>`,
"Array<boolean>",
"Array<unknown>",
`Array<Dict>`,
Expand Down Expand Up @@ -279,5 +281,3 @@ export const TYPE_TOKEN_ALL = [
...TYPE_TOKEN_PARAM_STR,
...TYPE_TOKEN_PARAM_TIME
] as const;


9 changes: 6 additions & 3 deletions src/types/runtime-types/SimpleType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,12 @@ export type SimpleTypeArray<T extends SimpleArrayToken> = T extends "Array"
? any[]
: T extends "Array<string>"
? string[]
: T extends `Array<string(${infer StringUnion})>`
? Array<SimpleTypeScalar<`string(${StringUnion})`>>
: T extends "Array<number>"
? number[]
: T extends `Array<number(${infer NumericUnion extends `${number}`})>`
? Array<SimpleTypeScalar<`number(${NumericUnion})`>>
: T extends "Array<boolean>"
? boolean[]
: T extends "Array<unknown>"
Expand All @@ -122,12 +126,14 @@ export type SimpleTypeArray<T extends SimpleArrayToken> = T extends "Array"
export type SimpleTypeMap<T extends SimpleMapToken> = T extends "Map"
? Map<any,any>
: T extends `Map<${infer Key extends MapKeys}, ${infer Value extends MapValues}>`
// eslint-disable-next-line no-use-before-define
? Map<SimpleType<Key>, SimpleType<Value>>
: never;

export type SimpleTypeSet<T extends SimpleSetToken> = T extends "Set"
? Set<any>
: T extends `Set<${infer Type extends SetTypes}>`
// eslint-disable-next-line no-use-before-define
? Set<SimpleType<Type>>
: never;

Expand Down Expand Up @@ -160,6 +166,3 @@ export type SimpleType<T extends SimpleToken> = T extends SimpleScalarToken
: never;

export type StructuredStringType = any;



18 changes: 18 additions & 0 deletions tests/runtime/SimpleType.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ describe("SimpleType<T>", () => {
];
});

it("String and Numeric literals", () => {
type Answer = SimpleType<"string(42)">;
type Answer2 = SimpleType<"number(42)">;

type FooBar = SimpleType<"string(foo,bar)">;
type OneTwo = SimpleType<"number(1,2)">;

type cases = [
Expect<Equal<Answer, "42">>, //
Expect<Equal<Answer2, 42>>, //
Expect<Equal<FooBar, "foo" | "bar">>, //
Expect<Equal<OneTwo, 1 | 2>>, //
];
const cases: cases = [
true, true, true, true
];
})

it("optional types", () => {
type Str = SimpleType<"Opt<string>">;
type Num = SimpleType<"Opt<number>">;
Expand Down

0 comments on commit 36accd4

Please sign in to comment.