0

The Dream:

 interface foo {
    buzz: string;
    das: number;
    bar: string[];
 }

 function findAllProperties(randomObject: any): string[] {
    // someone more versed in TS and smarter than me?
 }

This would be used for the first step in an 'assert all' function for properties on a given type, so when I pass a prop into a component I could assert all for not being null | undefined. I can't find any way to do this, and the others are in the belief its impossible.

Wikkle_A
  • 145
  • 2
  • 12
  • 2
    I don't understand what you're asking. Please elaborate. – JB Nizet Nov 14 '17 at 22:16
  • 2
    Those types do not exist at runtime, so you cannot call a function and check if buzz is a string. Type checking only exists at compile time and works automatically, so that you don't NEED an assert function at runtime. – Kokodoko Nov 14 '17 at 22:16
  • Duplicate https://stackoverflow.com/a/14426274/251311 ? – zerkms Nov 14 '17 at 22:17
  • 1
    @Kokodoko yea I've voiced that typescript compile checks, he then found the one way to pass a null into a prop without TS complaining by using a function(){}:any even though my tslint won't even allow any. just looking for a way to clean up my code without 5 lines of assertions in every component. You gave me a good laugh and I passed it on to bossman haha, thank you. – Wikkle_A Nov 14 '17 at 22:25
  • @Wikkle_A why do you use `any` as a type instead of `foo` ? Also, considering what you said about `null` : have you enabled `strictNullChecks` mode in your `tsconfig` ? – Jules Sam. Randolph Nov 14 '17 at 22:39
  • @Jules Randolph because I'd like to write a function that can take any interface and then spit back a string[] representing its properties, not just my 'foo' interface. And yes, I have strictNullChecks so 'any' doesn't even work within the project :/ – Wikkle_A Nov 14 '17 at 22:44
  • @Wikkle_A perhaps you looking for https://github.com/kimamula/ts-transformer-keys – Jules Sam. Randolph Nov 14 '17 at 22:46
  • I literally said 'omg' outloud. That looks amazing, I'll be taking a look – Wikkle_A Nov 14 '17 at 22:46
  • @Wikkle_A typescript custom transformers is a branding new, really promising feature ! Enjoy :-) – Jules Sam. Randolph Nov 14 '17 at 22:49
  • @Wikkle_A however I can't figure out how you might extract the interface from the object at runtime... I'm not sure I understand the use-case :/ – Jules Sam. Randolph Nov 14 '17 at 22:53

0 Answers0