Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ts-nameof-proxy

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-nameof-proxy

`nameof` in TypeScript, no compiler required. Implemented using proxy with some limitations.

  • 1.0.1
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
41
decreased by-44.59%
Maintainers
1
Weekly downloads
 
Created
Source

ts-nameof-proxy

nameof in TypeScript, no compiler required. Implemented using proxy with some limitations.

Type safely generate property name or path of variable, referenced properties can be renamed in bulk by the editor.

Install

npm install ts-nameof-proxy

Usage

nameOf(student, (s) => s.age); // "age"
nameOf(student, (s) => s.name.length); // "length"
nameOf<Student>((s) => s.name.length); // "length"

pathOf(student, (s) => s.age); // ["age"]
pathOf(student, (s) => s.name.length); // ["name", "length"]
pathOf<Student>((s) => s.name.length); // ["name", "length"]

pathStringOf(student, (s) => s.name.firstName[0]); // "['name']['firstName']['0']"
pathStringOf<Student>((s) => s.name.firstName[0]); // "['name']['firstName']['0']"

namesOf(student, (s) => (s.age, s.name.length)); // ["age", "length"]
namesOf<Student>((s) => (s.age, s.name.length)); // ["age", "length"]

pathsOf(student, (s) => (s.age, s.name.length)); // [["age"], ["name", "length"]]
pathsOf<Student>((s) => (s.age, s.name.length)); // [["age"], ["name", "length"]]

pathStringsOf(student, (s) => (s.name, s.name.firstName[0])); // ["['name']", "['name']['firstName']['0']"]
pathStringsOf<Student>((s) => (s.name, s.name.firstName[0])); // ["['name']", "['name']['firstName']['0']"]

Limitations

The example below is NOT feasible:

nameOf(student); // ❌ Will throw error
nameOf(student, (student) => student); // ❌ Will throw error

Example

const people = [
	{ name: { firstName: "John", lastName: "Doe" } },
	{ name: { firstName: "Jane", lastName: "Smith" } },
];

<Formik initialValues={people} onSubmit={() => {}}>
	<Form>
		{({ values }) =>
			values.map((person, index) => (
				<div key={person.name.firstName}>
					<Field
						name={pathStringOf(
							values,
							(values) => values[index].name.firstName
						)}
					/>
					<Field
						name={pathStringOf(values, (values) => values[index].name.lastName)}
					/>
				</div>
			))
		}
		{/*
      <Field name="['0']['name']['firstName']" />
      <Field name="['0']['name']['lastName']" />
      <Field name="['1']['name']['firstName']" />
      <Field name="['1']['name']['lastName']" />
    */}
	</Form>
</Formik>;

Keywords

FAQs

Package last updated on 17 Sep 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc