Skip to content

Commit

Permalink
Merge branch 'release/v0.0.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
manuth committed Sep 10, 2023
2 parents 0321c21 + dcb06c8 commit dde72de
Show file tree
Hide file tree
Showing 54 changed files with 1,312 additions and 682 deletions.
24 changes: 1 addition & 23 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,5 @@ module.exports = {
join(__dirname, "tsconfig.editor.json"),
join(__dirname, "tsconfig.eslint.json")
]
},
rules: {
"@typescript-eslint/no-unused-vars": [
"warn",
{
args: "none",
destructuredArrayIgnorePattern: "^_"
}
]
},
overrides: [
{
files: [
"*.test-d.ts"
],
rules: {
"jsdoc/require-jsdoc": "off",
"@delagen/deprecation/deprecation": "off",
"@typescript-eslint/naming-convention": "off",
"@typescript-eslint/no-namespace": "off"
}
}
]
}
};
22 changes: 21 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,27 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## TypeScript `nameof` [Unreleased]

[Show differences](https://github.com/typescript-nameof/nameof/compare/v0.0.2...dev)
[Show differences](https://github.com/typescript-nameof/nameof/compare/v0.0.3...dev)

## TypeScript `nameof` v0.0.3
### Added
- A function `nameof.typed` to get strictly typed key names
What, you don't need such a thing!? idc, I do!

Usage:
```ts
nameof.typed<Console>().warn;
// or
nameof.typed(() => process).exit;
// or
nameof.typed((module: NodeJS.Module) => module.require).resolve;
```

### Updated
- All dependencies
- Babel components for streamlining node transformation order

[Show differences](https://github.com/typescript-nameof/nameof/compare/v0.0.2...v0.0.3)

## TypeScript `nameof` v0.0.2
### Fixed
Expand Down
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ This section provides a brief explanation as to what functionalities are covered
- [Table of Contents](#table-of-contents)
- [Syntax](#syntax)
- [`nameof`](#nameof)
- [`nameof.typed`](#nameoftyped)
- [`nameof.full`](#nameoffull)
- [`nameof.interpolate`](#nameofinterpolate)
- [`nameof.array`](#nameofarray)
Expand Down Expand Up @@ -103,6 +104,50 @@ console.log(nameof<ITest>((x) => x.prop)); // Prints `prop`
console.log(nameof<ITest>((x) => x["prop"])); // Prints `prop`
```

### `nameof.typed`
The return type of the `nameof` call is `string`.
In some cases you might want to have the type of the `nameof` result set to its constant value rather than `string`.

The `nameof.typed()` function allows you to do exactly this.

The `typed` function requires either a type argument or a function returning the object to get a key for.
You can get the constantly typed property name by accessing the corresponding property of the `typed` call.

Examples:
```ts
class Test {
Hello() { }
World() { }
}

class Nested {
Inner: Test;
}

let a = nameof.typed<Test>().Hello; // `a` has the type `"Hello"`
let b = nameof.typed<Test>().World; // `b` has the type `"World"`
let c = nameof.typed<Nested>().Inner; // `c` has the type `"Inner"`
let d = nameof.typed((nested: Nested) => nested.Inner).Hello; // `d` has the type `"Hello"`
```

Having TypeScripts type checker know what exact type the `nameof.typed` result has, allows the use of the `nameof.typed` expressions to be used as element accessors.

Example

```ts
class Unifier {
Add(x: number, y: number) { return x + y; }
Concatenate(x: string, y: string) { return x + y; }
}

let unifier = new Unifier();
unifier[nameof.typed<Unifier>().Add](4, 2); // Returns `6`.
unifier[nameof.typed<Unifier>().Add]("Hello", "World"); // Reports an error because a `number` is expected.

unifier[nameof.typed<Unifier>().Concatenate]("Hello", "World"); // Returns `HelloWorld`
unifier[nameof.typed<Unifier>().Concatenate](4, 2); // Reports an error because a `string` is expected.
```

### `nameof.full`
Unlike `nameof()` calls, the `nameof.full` call allows you to print the full name of an expression as a `string`:

Expand Down
Loading

0 comments on commit dde72de

Please sign in to comment.