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.

How to get properties of {[key in keyof T]: T[key]} type?

See original GitHub issue
import { getType, Type } from "tst-reflect"
export class Temp {
    id: string
    email: string
}
type T = {
    [key in keyof Temp]: Temp[key]
}
function foo<T>() {
    const type: Type = getType<T>()
    console.log(type)
}
foo<T>()

Following code produces

this output
TypeActivator {
  _name: 'T',
  _fullName: 'T',
  _kind: 5,
  _constructors: [],
  _properties: [],
  _methods: [],
  _decorators: [],
  _typeParameters: [],
  _ctor: undefined,
  _ctorDesc: ConstructorImportActivator {},
  _interface: undefined,
  _isUnion: false,
  _isIntersection: false,
  _types: [],
  _literalValue: undefined,
  _typeArgs: [],
  _conditionalType: undefined,
  _indexedAccessType: undefined,
  _genericTypeConstraint: undefined,
  _genericTypeDefault: undefined,
  _baseType: TypeActivator {
    _name: 'Object',
    _fullName: 'Object',
    _kind: 2,
    _constructors: [],
    _properties: [],
    _methods: [],
    _decorators: [],
    _typeParameters: [],
    _ctor: [Function: ctor],
    _ctorDesc: ConstructorImportActivator {},
    _interface: undefined,
    _isUnion: false,
    _isIntersection: false,
    _types: [],
    _literalValue: undefined,
    _typeArgs: [],
    _conditionalType: undefined,
    _indexedAccessType: undefined,
    _genericTypeConstraint: undefined,
    _genericTypeDefault: undefined,
    _baseType: undefined
  }
}
As I can see it doesn't contain neither properties nor types. Is it bug or feature or do I use it in wrong way?

Does it work with Omit and Pick? (I see the same output for them because they are implemented in similar way I did it in example)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Igorjan94commented, Aug 15, 2022

Ah yeah, I see. Didn’t notice that typescript throws it away in indexer (anyway, I don’t use these indexers now, it was added for migrating)

Thanks for making this amazing package, really appreciate the effort!

1reaction
Igorjan94commented, Aug 15, 2022

Works like a charm!

Only bug I found during some testing, that Omit fails when using [key: string]: any extension in class:

function foo<T>() {
    const type: Type = getType<T>()
    console.log(type)
}

export class Temp {
    id?: string
    email?: string
//    [key: string]: any //  <--------
}

foo<Omit<Temp, 'email'>>()
Example output
TypeActivator {
  _name: 'Omit',
  _fullName: 'Omit',
  _kind: 5,
  _constructors: [],
  _properties: [],
  _indexes: [
    IndexInfo {
      _keyType: [LazyType],
      _type: [LazyType],
      readonly: false
    },
    IndexInfo {
      _keyType: [LazyType],
      _type: [LazyType],
      readonly: false
    }
  ],
  _methods: [],
  _decorators: [],
  _typeParameters: [],
  _ctor: undefined,
  _ctorDesc: ConstructorImportActivator {},
  _interface: undefined,
  _isUnion: false,
  _isIntersection: false,
  _types: [],
  _literalValue: undefined,
  _typeArgs: [],
  _conditionalType: undefined,
  _indexedAccessType: undefined,
  _functionType: undefined,
  _genericTypeConstraint: undefined,
  _genericTypeDefault: undefined,
  _isGenericType: false,
  _genericTypeDefinition: undefined,
  _baseType: LazyType {
    typeResolver: [Function (anonymous)],
    resolvedType: TypeActivator {
      _name: 'Object',
      _fullName: 'Object',
      _kind: 2,
      _constructors: [],
      _properties: [],
      _indexes: [],
      _methods: [],
      _decorators: [],
      _typeParameters: [],
      _ctor: [Function: ctor],
      _ctorDesc: ConstructorImportActivator {},
      _interface: undefined,
      _isUnion: false,
      _isIntersection: false,
      _types: [],
      _literalValue: undefined,
      _typeArgs: [],
      _conditionalType: undefined,
      _indexedAccessType: undefined,
      _functionType: undefined,
      _genericTypeConstraint: undefined,
      _genericTypeDefault: undefined,
      _isGenericType: false,
      _genericTypeDefinition: undefined,
      _baseType: undefined
    }
  }
}

If marked line is uncommented, then type is wrong, otherwise $-$ works as intended

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use the keyof operator in TypeScript - LogRocket Blog
In JavaScript, we often use Object.keys to get a list of property keys. ... keyof T returns a union of string literal types....
Read more >
Typescript using get<T>(key: keyof T) - Stack Overflow
To be able to infer property type based on provided key we need to introduce generic type parameter for key. Currently partial type...
Read more >
Documentation - TypeScript 2.1
Enter Index Type Query or keyof ; An indexed type query keyof T yields the type of permitted property names for T ....
Read more >
Help getting type of key of generic : r/typescript - Reddit
Here's what I'm trying to accomplish type MyType = { a: string, b: number } interface PatchOperation { property: keyof T, ...
Read more >
Advanced uses of Typescript type system 01 - From mapped ...
What they don't allow is having generic property names. ... A way to get keyof only for certain types of keys type KeyofMethods<TType>...
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