1

I can't think of a way to filters class properties by type as in the below code. And store it on a type not JavaScript just for the TypeScript part

class Test 
{
  func(){}
  age:number = 5
  name:string = '<my-name>'
}
type ClassPropetiesByType<ClassType,PropertiesTypes> = null // code will be here
type TestFunctions = getClassPropetiesByType<Test, Function> // => 'func'
type TestStrings = getClassPropetiesByType<Test, string> // => 'name'
type TestNumbers = getClassPropetiesByType<Test, number> // => 'age'
Mor Bargig
  • 198
  • 1
  • 7
  • 1
    See the definition of `KeysMatching` in the other question/answer pair. Using that with your class gives [this code](https://tsplay.dev/wOPRRN). Good luck! – jcalz Apr 09 '22 at 18:11
  • 1
    If it helps, this was my answer: [Playground](https://www.typescriptlang.org/play?#code/MYGwhgzhAEAqCmEAu0DeBYAUNaAzArgHbAAUAlGtAL5Y5gDm8AXNIfgLYBG8ATtALzQArLWgB3APY8A1gAsJ+HhBZsuvAdACMABlGEw7ZtGQ8AloXoaA5AB52ATwC0+wwD4rWGpixJ7AB3hoAGFwKAAFHgkApFNEACF7WH94GxDICCSA6HgADyR4QgATGAlOACt4YCQAGmgIqN4YxEzEVw1peHsJXDRRAG0AaWhzaA6unrSoFuhIYNCM5MGAXWy8guK6yICeJoWAmAB+aCGVeAA3XiWWSb34ZYBuTywfZLhEJAAxIirTCUIYQQ3erRWIQBItGwIZC1L7EGJ-NoAekRAjaVgIxA82BwOGR0AAegcXlkoUgAMpIMwWAFzdLA+C7cHJSHvWomcz0JEo-holzwLE43EownEwKkgByHG4Sg0QK2DNBTICLOhrClvC5qOgVgY-OgAB9tZIZPJFBABYK8YSgA) – Daniel Rodríguez Meza Apr 09 '22 at 18:12
  • Hi, thanks guys, i need one more thing. This solution not working with private properties – Mor Bargig Apr 10 '22 at 09:49

0 Answers0