npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ts-partial-type-resolver

v0.2.0

Published

[![Coverage Status](https://coveralls.io/repos/github/svenliebig/ts-partial-type-resolver/badge.svg?branch=master)](https://coveralls.io/github/svenliebig/ts-partial-type-resolver?branch=master)

Downloads

7

Readme

TypeScript Partial Type Resolver

Coverage Status

Under Construction 🏗

But feel free to look around!


This project has it's motiviation from creating mock data. I want to resolve types without compiling the whole project, and just load these types that I need. The goal is to resolve types like this:

type Person = {
	address: Address
	name: string
}

Into types like this:

type Person = {
	address: {
		street: string
		state: string
		city: string
		postalcode: number
	}
	name: string
}

Usage

Let's assume the following structure (You can also see the sources of that example here.

// state.ts
export enum State {
	ALABAMA = "AL",
	CALIFORNIA = "CA",
	NEVADA = "NV",
}

// language.ts
export enum Language {
	ENGLISH = "EN",
	GERMAN = "DE",
}

// address.ts
import { State } from "./state"

export type Address = {
	street: string
	state: State
	city: string
	postalcode: number
}

// person.ts
import { Language } from "./language"
import { Address } from "./address"

type Person = {
	name: string
	surname: string
	address: Address
	languages: Array<Language>
	employed: boolean
}

You can execute the parser like that:

import { Parser, prettify } from "ts-partial-type-parser"

const path = resolve(__dirname, "person.ts")
const parser = new Parser(path)
const person = parser.resolve("Person")

console.log(prettify(person.toString()))

that will log:

type Person = {
	name: string
	surname: string
	address: {
		street: string
		state: State
		city: string
		postalcode: number
	}
	languages: Array<Language>
	employed: boolean
}

it resolves the type. So you can also rewrite the type, with rewrite:

import { Parser, prettify, rewrite } from "ts-partial-type-parser"

const path = resolve(__dirname, "person.ts")
const parser = new Parser(path)
const person = parser.resolve("Person")

const rewrittenPerson = rewrite(person.type, {
	boolean(type) {
		return "faker.boolean()"
	},
	string(type) {
		return "faker.string()"
	},
	enum(type) {
		// take the first one
		return `${type.name}.${type.members.keys().next().value}`
	},
	number(type) {
		return "faker.number()"
	},
	reference(type) {
		return type.toString()
	},
})

console.log(`const mock = ${prettify(rewrittenPerson)}`)

this will output:

const mock = {
	name: faker.string(),
	surname: faker.string(),
	address: {
		street: faker.string(),
		state: State.ALABAMA,
		city: faker.string(),
		postalcode: faker.number(),
	},
	languages: [Language.ENGLISH],
	employed: faker.boolean(),
}

That way you can create mocking types or rewriting types!

Supported

  • [x] - string
  • [x] - number
  • [x] - Date
  • [x] - string & number literal
  • [x] - objects (type literal)
  • [x] - union types
  • [x] - type reference inside the same file
  • [x] - type reference from an import
  • [x] - Array<TYPE>
  • [x] - TYPE[] - Array
  • [x] - Intersection
  • [x] - Enums
  • [x] - null
  • [x] - Configuration to define types that should not be resolved

TODO

  • [ ] - Parenthesiszed Types
  • [ ] - Boolean
  • [ ] - import * as Type
  • [ ] - export * from
  • [ ] - Interface
  • [ ] - Extends
  • [ ] - Pick
  • [ ] - void
  • [ ] - library imports
  • [ ] - undefined
  • [ ] - function
  • [ ] - ? optional properties
  • [ ] - Omit
  • [ ] - [string, number]
  • [ ] - Looped types create a loop in the code