inferred-types
Advanced tools
Comparing version 0.2.0 to 0.3.0
{ | ||
"name": "inferred-types", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Functions which provide useful type inference on TS projects", | ||
@@ -22,4 +22,6 @@ "license": "MIT", | ||
"@ampproject/rollup-plugin-closure-compiler": "^0.26.0", | ||
"@antfu/eslint-config-ts": "^0.6.2", | ||
"@rollup/plugin-commonjs": "^17.0.0", | ||
"@rollup/plugin-node-resolve": "^11.0.1", | ||
"@type-challenges/utils": "~0.1.1", | ||
"@types/jest": "^26.0.20", | ||
@@ -53,4 +55,3 @@ "@types/node": "^14.14.22", | ||
"typescript": "^4.1.3", | ||
"typescript-transform-paths": "~2.2.2", | ||
"@type-challenges/utils": "~0.1.1" | ||
"typescript-transform-paths": "~2.2.2" | ||
}, | ||
@@ -60,2 +61,2 @@ "dependencies": { | ||
} | ||
} | ||
} |
@@ -7,3 +7,3 @@ # Inferred Types | ||
- `Model` - wraps the popular `io-ts` codec/type class in a way that ensures that the model's name is seen as a _literal type_ rather than just being represented as a `string` as it is in `io-ts`. | ||
- `Model` - wraps the popular `io-ts` codec/type class in a way that ensures that the model's name is seen as a _literal type_ rather than just a `string` as it is in `io-ts`. This helps TS's inference downstream. | ||
- `Configurator` - provides a configurator which is intended to allow a type strong dictionary to be built up as part of a builder pattern | ||
@@ -18,1 +18,22 @@ - `FluentConfigurator` - provides a fluent style API which also provides a similar feature set to the base `Configurator`. | ||
All symbols are typed and exported in both CommonJS and ESModule format. | ||
## Devops | ||
This repo uses **pnpm** to bring in all deps. Please use that instead of **npm**, **yarn**, or whatever your favorite package manager is these days. The main things you will be interested in while working this repo are: | ||
- `pnpm build` - transpiles the Typescript source to javascript and typings files off of /dist; it uses Rollup to do this. | ||
- `pnpm lint` - linting is part of the build process but sometimes you just want see the lint results without the parts of the build done | ||
- `pnpm build:bundle` - if you have things which are not passing yet lint muster but you need to just test the executable code _as-is_ is building you can do that with this target | ||
## Contributing | ||
Contributions are welcome as pull requests. To aid in making this process efficient, please note: | ||
- We use the _git-flow_ standard for branch naming and therefore, if you're wanting to merge in a PR please target it at the **@develop** branch and we will review it before merging it in. Once in develop we'll run it through CI-CD there and merge it into @master and deploy a new version of **npm**. | ||
- New code requires _new_ (or at least _updated_) test cases to demonstrate both the intended effect and ensure this effect is preserved into the future; please expect us to reject the PR if there is no attempt to show | ||
**Note:** all our coding standards have been hopefully incorporated as **eslint** rules and therefore stylistically so long as you're saving using these rules along with prettier we should be in good shape for the PR review to just focus on the "real stuff". | ||
@@ -29,2 +29,3 @@ /** | ||
// TODO: create a function which takes the unique value as a parameter instead of having static functions above | ||
/** | ||
@@ -34,8 +35,8 @@ * Takes an array of type `<T>` and spreads it out into a dictionary while preserving the type | ||
*/ | ||
export function discriminatedArrayToDictionary<T extends {}>(arr: readonly T[]) { | ||
return <K extends keyof T & PropertyKey>(key: K) => { | ||
const keys = arr.map((i) => i[key]); | ||
// export function discriminatedArrayToDictionary<T extends {}>(arr: readonly T[]) { | ||
// return <K extends keyof T & PropertyKey>(key: K) => { | ||
// const keys = arr.map((i) => i[key]); | ||
return arr.reduce((acc, v) => ({ ...acc, [key]: v }), {} as { [V in T as V[K]]: V }); | ||
}; | ||
} | ||
// return arr.reduce((acc, v) => ({ ...acc, [key]: v }), {} as { [V in T as V[K]]: V }); | ||
// }; | ||
// } |
@@ -5,2 +5,13 @@ import * as t from "io-ts"; | ||
/** | ||
* Defines an `io-ts` based **model**. | ||
* | ||
* - `<T>` represents type of the model | ||
* - `<S>` is the model's literal type | ||
* | ||
* Note: unlink the default `io-ts` model/codec, the | ||
* `name` variable is an explicit type alias and not a string | ||
* type. This helps to preserve it's type inference even though | ||
* the model itself does have a runtime definition of it's type. | ||
*/ | ||
export type IModel<M, N extends string> = Omit<t.Type<M>, "name"> & { name: N }; | ||
@@ -7,0 +18,0 @@ |
@@ -1,6 +0,6 @@ | ||
import { arrayToObjectName } from "~/arrayToObject"; | ||
import * as t from "io-ts"; | ||
import { Equal, Expect, ExpectExtends, NotEqual } from "@type-challenges/utils"; | ||
import { SimpleTable } from "./data"; | ||
import { IModel, Model } from "~/Model"; | ||
import { Equal, Expect, ExpectExtends, NotEqual } from "@type-challenges/utils"; | ||
import { arrayToObjectName } from "~/arrayToObject"; | ||
@@ -16,3 +16,3 @@ const SongMeta = t.partial({ year: t.number, genre: t.string }); | ||
const Playlist = Model("Playlist", { name: t.string, owner: t.string }); | ||
type PlaylistType = t.TypeOf<typeof Playlist>; | ||
// type PlaylistType = t.TypeOf<typeof Playlist>; | ||
@@ -80,4 +80,2 @@ describe("arrayToObject => ", () => { | ||
type SimpleTableReturn = ReturnType<typeof SimpleTable>; | ||
// @ts-ignore | ||
@@ -84,0 +82,0 @@ type cases = [ |
import * as t from "io-ts"; | ||
import { Model } from "~/Model"; | ||
export const PlaylistRequired = t.type({ | ||
export const PlaylistRequired = { | ||
/** name of the playlist */ | ||
@@ -8,8 +9,8 @@ name: t.string, | ||
description: t.string, | ||
}); | ||
export const PlaylistOptional = t.partial({ | ||
}; | ||
export const PlaylistOptional = { | ||
/** artists included in the playlist */ | ||
artists: t.array(t.string), | ||
}); | ||
}; | ||
export const Playlist = t.intersection([PlaylistRequired, PlaylistOptional], "Playlist"); | ||
export const Playlist = Model("Playlist", PlaylistRequired, PlaylistOptional); |
@@ -11,3 +11,3 @@ import { IModel } from "~/Model"; | ||
export const SimpleTable = <T extends { name: S }, S extends Readonly<string>>( | ||
export const SimpleTable = <T extends {}, S extends Readonly<string>>( | ||
model: IModel<T, S> | ||
@@ -14,0 +14,0 @@ ): ISimpleTable<T, S> => { |
import * as t from "io-ts"; | ||
import { Model } from "~/Model"; | ||
export const SongMeta = t.partial({ year: t.number, genre: t.string }); | ||
export const Song_RequiredProps = t.type({ | ||
export const Song_RequiredProps = { | ||
song: t.string, | ||
artist: t.string, | ||
}); | ||
export const Song_OptionalProps = t.partial({ | ||
}; | ||
export const Song_OptionalProps = { | ||
/** the album which the song is featured in */ | ||
@@ -13,6 +14,5 @@ album: t.string, | ||
meta: SongMeta, | ||
}); | ||
}; | ||
export const Foo = t.type({ id: t.string, name: t.string }, "Foo"); | ||
export const Song = t.intersection([Song_RequiredProps, Song_OptionalProps], "Song"); | ||
export const Song = Model("Song", Song_RequiredProps, Song_OptionalProps); | ||
export type ISong = t.TypeOf<typeof Song>; | ||
@@ -25,2 +25,1 @@ export const mySong: ISong = { | ||
export const Songs = t.array(Song); | ||
export const MixedSong: t.Mixed = Song; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
561144
84
38
34
1963