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-typed

v1.3.6

Published

A lightweight library that add typing at runtime for typescript projects

Downloads

201

Readme

codecov Build Status FOSSA Status

Description

Typed is a lighweight library that aims to add typings at runtime.

How to use it

Install using npm $ npm install ts-typed --save or using $ yarn add ts-typed --save

  • Using TypedFactory.create(data, Model) allows you to return data in a given Model structure

  • Using @Typed property decorator.

Good to know

When fetching data from network on typescript project, the data is typed as Object on the Runtime. Typescript cares about compilation, but sometimes you need to get typed on the Runtime. In order to do that you need to know that the only way is to instantiate typescript class. However the subclasses will not be typed if you dont explicitly instantiate them, that's where @Typed is helpfull.

Notice two recommended things:

  • The use of Partial in order get flexible constuctor params.
  • The use of ? for optional param in order to get also acces the a no params constructor.
import { Typed, TypedSerializer } from 'typed';

export class SubClazz {
  numberField: number;
  stringField: string;
  booleanField: boolean;
  constructor(obj?: Partial<SubClazz>) {
    Object.assign(this, obj);
  }
}

Without @Typed decorator :

class SuperTypeClazz {
  numberField: number;
  stringField: string;
  booleanField: boolean;
  complexTypeField: SubClazz;

  constructor(obj?: Partial<SuperTypeClazz>) {
    Object.assign(this, obj, {
      complexTypeField : new SubClazz(obj?.complexTypeField)
    });
  }
}

With @Typed decorator :

class SuperTypeClazz {
  numberField: number;
  stringField: string;
  booleanField: boolean;
  
  @Typed(SubClazz);
  complexTypeField: SubClazz;

  constructor(obj?: Partial<SuperTypeClazz>) {
    Object.assign(this, obj);
  }
}

Serialization

Since @Typed rewrites Getter and Setter and renames the property with an underscore _ prefix. Sometime it can be touchy to get this when sending data to the server. That's why ts-typed provides the TypedSerializer.serialize method. You only have to define the toJSON method like the example below :

Example

import { Typed, TypedSerializer } from 'typed';

abstract class AbstractTypedModel{
}

export class AccountModel extends AbstractTypedModel {

  id: string;
  uri: string;
  username: string;
  provider: string;
  avatar: string;

  constructor(parameters?: Partial<AccountModel>) {
    super();
    const {id, uri, username, provider, avatar} = parameters;
    this.id = id;
    this.uri = uri;
    this.username = username;
    this.provider = provider;
    this.avatar = avatar;
  }
}

export class PersonModel {

  id: string;
  lastname: string;
  firstname: string;

  @Typed(AccountModel)
  account: AccountModel;

  constructor(parameters?: Partial<PersonModel>) {
    const {id, lastname, firstname, account} = parameters;
    this.id = id;
    this.lastname = lastname;
    this.firstname = firstname;
    this.account = account;
  }

  whatIsYourName(): void {
    console.log('A girl has no name');
  }

  toJSON(): PersonModel {
     return TypedSerializer.serialize(this);
  }
}

Console capture Using this way, you'll be sure to get the model at runtime, and then call method, get subtypes, etc.