Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Name of properrty #1

Closed
scinfu opened this issue Mar 18, 2022 · 4 comments
Closed

Name of properrty #1

scinfu opened this issue Mar 18, 2022 · 4 comments

Comments

@scinfu
Copy link

scinfu commented Mar 18, 2022

Can I access to name property of class or interface?
like:

import { keyof } from "ts-keyof"
interface IModel {
  id: string
  name: string
}
console.log("Name of property",keyof(IModel.name)))
@sotnikov-link
Copy link
Owner

@scinfu

It's not possible, because IModel is TypeScript Interface. TypeScript Entities doesn't exist in runtime.

I can add keyof<IModel>("name") that will be give you "Name of property". Do you want it?

@scinfu
Copy link
Author

scinfu commented Mar 21, 2022

@sotnikov-link Yes, if the name "name" will wrong ES will detect the error?
es: keyof<IModel>("namex")

@sotnikov-link
Copy link
Owner

sotnikov-link commented Mar 21, 2022

@scinfu if you use function below then TypeScript gives error for your case:

function getKeyOf<T extends object>(keyName: keyof T) {
  return keyName;
}

interface IModel {
  id: string
  name: string
}

getKeyOf<IModel>("name") === "name" // 👍

getKeyOf<IModel>("wrongKey") // 👎  Argument of type '"wrongKey"' is not assignable to parameter of type 'keyof IModel'.(2345)

TypeScript Play

This function doesn't work for TypeScript Refactoring so I don't want add this function in package ts-keyof.

@scinfu
Copy link
Author

scinfu commented Mar 22, 2022

@sotnikov-link nice.
Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants