ircraziesttaxi / ts-simple-nameof Goto Github PK
View Code? Open in Web Editor NEWParses a dot-separated property name from a lambda expression and provides some level of type safety using type parameters.
License: MIT License
Parses a dot-separated property name from a lambda expression and provides some level of type safety using type parameters.
License: MIT License
Our company has a shared library with style components to make sure different products have the same look. I'm experiencing an issue with the following piece of code when the project is compiled for ES5. When Es6 is used, it works fine.
public ngOnChanges(changes: SimpleChanges): void {
if (changes[nameof(() => this.min)]) {
this.minDateTime = this.toStartOf(this.min);
}
}
The underlying issue is that the regex used in the ES5 prop selector in this library does not support the use of anonymous functions. In Javascript, the input for the nameof-function is function(){return t.min}
. However, the regex expects the format function (x) { return x.prop; }
.
My suggestion would be to make the argument optional by changing the regex to /function\s*\([\w+]?\)\s*\{[\r\n\s]*return\s+\w+\.((\w+\.)*(\w+))/i
Issue:
Using nameof throws error ts-simple-nameof: Invalid function.
Issue is with how the parser works.
if (fnStr.startsWith("function (")) {
will not capture functions with format
function(x) { return x.value }
Lack of space between function and left parentheses.
Resolution.
Can be resolved by adding the extra condition.
I can raise a PR for this if required.
Got the following code:
export enum TicketPriorityEnum {
'Low' = 'Low',
'Medium' = 'Medium',
'High' = 'High',
'Critical' = 'Critical'
}
...
import { TicketPriorityEnum } from '../../services';
nameof(TicketPriorityEnum)
And the following error:
Error TS2769 (TS) No overload matches this call.
Overload 1 of 2, '(selector: (obj: Object) => any, options?: NameofOptions): string', gave the following error.
Argument of type 'typeof TicketPriorityEnum' is not assignable to parameter of type '(obj: Object) => any'.
Type 'typeof TicketPriorityEnum' provides no match for the signature '(obj: Object): any'.
Overload 2 of 2, '(classType: new (...params: any[]) => Object): string', gave the following error.
Argument of type 'typeof TicketPriorityEnum' is not assignable to parameter of type 'new (...params: any[]) => Object'.
Type 'typeof TicketPriorityEnum' provides no match for the signature 'new (...params: any[]): Object'.
I guess enums are not supported. Are they?
WARNING in home.component.ts depends on 'ts-simple-nameof'. CommonJS or AMD dependencies can cause optimization bailouts.
For more info see: https://angular.io/guide/build#configuring-commonjs-dependencies
It would be nice to be able to retrieve the name of a given interface, instead of only concrete classes or property selectors of an interface/class.
For example:
interface User {
firstName: string;
lastName: string;
}
const interfaceName = nameof<User>();
I haven't looked at the code yet, but I'm not sure if that would force an API change with the current nameof<TClass>(TClass)
signature. Based on the related package ts-nameof, I would imagine it's possible.
Hi,
Thanks for the library. I am maintaining of TypeScript AutoMapper and I also leverage getting the property path from a lambda function: d => d.foo.bar
.
I've been trying to make my library smaller. Today, I've looked at my current implementation of getting the property path and I happen to find this "simple" (pun-intended) library. I dropped the relevant code from this library in my AutoMapper and all the tests passed but one. Considering the following case:
nameOf<Foo>(f => {
return f.foo.bar;
});
Theoretically, the above case is valid JavaScript, and for whatever reason, it might happen. However, nameOf()
failed to extract foo.bar
because the path where nameOf()
checks for Arrow function =>
is a little too simple. In my library, I have this extra replace
replace(/(?:\s|;|{|}|\(|\)|)+/gm, '');
I'm not very good with RegExp so I'm creating this issue to notify you about this weird case and maybe you'll be able to come up with a better solution.
Cheers
In console error: nameof function is undefined
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.