diff --git a/types/ts-nameof/index.d.ts b/types/ts-nameof/index.d.ts index 1a4daec3dd..b555170404 100644 --- a/types/ts-nameof/index.d.ts +++ b/types/ts-nameof/index.d.ts @@ -1,19 +1,133 @@ -// Type definitions for ts-nameof 4.0 +// Type definitions for ts-nameof 4.2 // Project: https://github.com/dsherret/ts-nameof#readme // Definitions by: David Sherret // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +/** + * Gets a string representation of the final identifier of the given expression. + * + * @example nameof() -> "MyInterface" + * @example nameof>() -> "Array" + * @example nameof() -> "MyInnerInterface" + * @example nameof(o => o.prop) -> "prop" + * + * @param func An optional function for which the last identifier of the expression will be parsed. + */ // tslint:disable-next-line no-unnecessary-generics declare function nameof(func?: (obj: T) => void): string; + +/** + * Gets a string representation of the last identifier of the given expression. + * + * @example nameof(console) -> "console" + * @example nameof(console.log) -> "log" + * @example nameof(console["warn"]) -> "warn" + * + * @param obj An expression for which the last identifier will be parsed. + */ declare function nameof(obj: any): string; + declare namespace nameof { + /** + * Gets the string representation of the entire type parameter expression. + * + * @example nameof.full() -> "MyNamespace.MyInnerInterface" + * @example nameof.full(1) -> "MyInnerInterface" + * @example nameof.full>() -> "Array" + * @example nameof.full>(-1) -> "MyInnerInterface" + * + * @param periodIndex Specifies the index of the part of the expression to parse. + * When absent, the full expression will be parsed. + * A negative index can be used, indicating an offset from the end of the sequence. + */ // tslint:disable-next-line no-unnecessary-generics function full(periodIndex?: number): string; + + /** + * Gets the string representation of the entire resultant expression. + * + * @example nameof.full(o => o.prop.prop2) -> "prop.prop2" + * @example nameof.full(o => o.prop.prop2.prop3, 1) -> "prop2.prop3" + * @example nameof.full(o => o.prop.prop2.prop3, -1) -> `"prop3" + * + * @param func A function for which the result will be parsed, excluding the parameter's identifier. + * @param periodIndex Specifies the index of the part of the expression to parse. + * When absent, the full expression will be parsed. + * A negative index can be used, indicating an offset from the end of the sequence. + */ // tslint:disable-next-line no-unnecessary-generics function full(func: (obj: T) => void, periodIndex?: number): string; + + /** + * Gets the string representation of the entire given expression. + * + * @example nameof.full(console.log) -> "console.log" + * @example nameof.full(window.alert.length, -1) -> "length" + * @example nameof.full(window.alert.length, 2) -> "length" + * + * @param obj The expression which will be parsed. + * @param periodIndex Specifies the index of the part of the expression to parse. + * When absent, the full expression will be parsed. + * A negative index can be used, indicating an offset from the end of the sequence. + */ function full(obj: any, periodIndex?: number): string; + + /** + * Gets an array containing the string representation of the final identifier of each expression in the array returned by the provided function. + * + * @example nameof.toArray(o => [o.firstProp, o.otherProp.secondProp, o.other]) -> ["firstProp", "secondProp", "other"] + * @example nameof.toArray(o => [o.prop, nameof.full(o.myProp.otherProp, 1)]) -> ["prop", "myProp.otherProp"] + * + * @param func A function returning an array of expressions to be parsed, excluding the parameter's identifier. + */ // tslint:disable-next-line no-unnecessary-generics function toArray(func: (obj: T) => any[]): string[]; + + /** + * Gets an array containing the string representation of each expression in the arguments. + * + * @example nameof.toArray(myObject, otherObject) -> ["myObject", "otherObject"] + * @example nameof.toArray(obj.firstProp, obj.secondProp, otherObject, nameof.full(obj.other)) -> ["firstProp", "secondProp", "otherObject", "obj.other"] + * + * @param args An array of expressions to be parsed. + */ function toArray(...args: any[]): string[]; + + /** + * Embeds an expression into the string representation of the result of nameof.full. + * + * @example nameof.full(myObj.prop[nameof.interpolate(i)]) -> `myObj.prop[${i}]` + * + * @param value The value to interpolate. + */ function interpolate(value: T): T; + + /** + * Gets an array of strings where each element is a subsequent part of the expression provided. + * + * @example nameof.split(o => o.prop.prop2.prop3) -> ["prop", "prop2", "prop3"] + * @example nameof.split(o => o.prop.prop2.prop3, 1) -> ["prop2", "prop3"] + * @example nameof.split(o => o.prop.prop2.prop3, -1) -> ["prop", "prop2"] + * + * @param func A function for which the resultant parts will be parsed, excluding the parameter's identifier. + * @param periodIndex Specifies the index of the part of the expression to parse. + * When absent, the full expression will be parsed. + * A negative index can be used, indicating an offset from the end of the sequence. + */ + // tslint:disable-next-line no-unnecessary-generics + function split(func: (obj: T) => any, periodIndex?: number): string[]; + + /** + * Gets an array of strings where each element is a subsequent part of the expression provided. + * + * @example nameof.split(myObj.prop.prop2.prop3) -> ["myObj", "prop", "prop2", "prop3"] + * @example nameof.split(myObj.prop.prop2.prop3, -3);`, `["prop", "prop2", "prop3"]; + * @example nameof.split(myObj.prop.prop2.prop3, 2);`, `["prop2", "prop3"] + * + * @param obj An expression for which the parts will be parsed. + * @param periodIndex Specifies the index of the part of the expression to parse. + * When absent, the full expression will be parsed. + * A negative index can be used, indicating an offset from the end of the sequence. + */ + function split(obj: any, periodIndex?: number): string[]; } diff --git a/types/ts-nameof/ts-nameof-tests.ts b/types/ts-nameof/ts-nameof-tests.ts index 0b7878b3cd..b264a536ab 100644 --- a/types/ts-nameof/ts-nameof-tests.ts +++ b/types/ts-nameof/ts-nameof-tests.ts @@ -29,6 +29,12 @@ nameof.toArray(testInstance.prop1); // $ExpectType string[] nameof.toArray(testInstance.prop1, testInstance.prop2); // $ExpectType string[] nameof.toArray(t => [t.prop1]); // $ExpectType string[] +// nameof.split tests +nameof.split(testInstance.prop1); // $ExpectType string[] +nameof.split(testInstance.prop1, 1); // $ExpectType string[] +nameof.split(obj => obj.prop1); // $ExpectType string[] +nameof.split(obj => obj.prop1, 1); // $ExpectType string[] + // nameof.interpolate tests nameof.interpolate("" as string); // $ExpectType string