typescript-is icon indicating copy to clipboard operation
typescript-is copied to clipboard

Generated code duplication

Open wheercool opened this issue 3 years ago • 1 comments

import { is } from 'typescript-is';

interface IUser {
  name: string;
}

let user1: IUser = { name: 'Hello' },
    user2: IUser = {} as IUser;

if (is<IUser>(user1) && is<IUser>(user2)) {
  console.log(user1.name, user2.name)
}

In this example there are 2 uses of is with the same interface IUser. Current implementation inlines all usages of is, so it cause a code duplication. Is it possible to generate some kind of cache/registry where all definitions will be stored?`

wheercool avatar Oct 18 '20 20:10 wheercool

Hi @wheercool

At the moment this is not possible, except using for example the createIs function:

import { createIs } from 'typescript-is';

interface IUser {
  name: string;
}

const isUser = createIs<IUser>(); // Generate code for IUser once.

if (isUser(user1) && isUser(user2)) {
  ...
}

At the moment there is no "smart" caching or sharing of code between types that does this automatically.

woutervh- avatar Nov 25 '20 17:11 woutervh-