question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make this package work with generics

See original GitHub issue

I know in your README you mentioned that this doesn’t work with generic types, i.e.

keys<T>

This functionality would be incredibly useful to me if it worked - and I would love to help contribute if you have any ideas for how to get this to work.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
robbiemucommented, Dec 23, 2019

could you go into a bit more detail ? I’m noticing a pretty sweet use that would obviate the need to write factory methods per interface, for example:

export function factoryInterface<T>(template = <Partial<T>>{}): T {
  const keyCollection = keys<T>();
  const data = <Partial<T>>{};

  Object.keys(template)
    .filter(key => keyCollection.includes(<keyof T>key))
    .forEach(key => data[<keyof T>key] = template[<keyof T>key]);

  keyCollection.forEach(key => {
    if (!data.hasOwnProperty(key)) {
      data[key] = undefined
    }
  });

  return data as T;
}
0reactions
ShadiestGoatcommented, Jan 2, 2021

Ok ok I get that this wouldn’t really work, but is there a work around of sorts?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Java Generics Example Tutorial - Generic Method, Class ...
We use <T> to create a generic class, interface, and method. The T is replaced with the actual type when we use it....
Read more >
Generic Methods - Java™ Tutorials
This Java tutorial describes generics, full screen mode API, and Java certification related resources.
Read more >
Tutorial: Getting started with generics - The Go Programming ...
From the command prompt, create a directory for your code called generics. $ mkdir generics $ cd generics. Create a module to hold...
Read more >
Is it possible to access an instance of a generic package from ...
I'm writing a generic library. I have a protected object that needs to be accessed by code in two packages. Both the package...
Read more >
GitLab Generic Packages Repository
Generic package sample project. The Write CI-CD Variables in Pipeline project contains a working example you can use to create, upload, and download...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found