Skip to content

Commit

Permalink
fix typings (#21)
Browse files Browse the repository at this point in the history
- removed most any's
- more comments/documentation
- breaking change: removed generic from Enumerable type (it's not possible to differ between numeric and heterogeneous enum)
- breaking change: optimized EventArgs (EventArgs, especially CancelEventArgs, can now be created without parameters)
- updated dependencies
- cleaned, optimized, simplified, fixed typings
- new: scoping core
  • Loading branch information
maddin1502 authored Nov 13, 2024
1 parent 4dfe888 commit 06f89d0
Show file tree
Hide file tree
Showing 26 changed files with 818 additions and 195 deletions.
1 change: 1 addition & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "https://json.schemastore.org/prettierrc",
"singleQuote": true,
"trailingComma": "none"
}
95 changes: 84 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,22 @@
[![npm downloads](https://badgen.net/npm/dw/ts-lib-extended)](https://badge.fury.io/js/ts-lib-extended)

## Features
- Enum type
- Enum type (key and value extraction - ignores reveres mapping)
- Dictionary types (safe, readonly, key + value types)
- Constructor types (abstract, standard, parameter + instance types)
- Core class for disposable instances
- Events (handler, args)
- Events (handler, args, cancellation, external subscription + internal invocation)
- Array types (minimal length array, item type)
- Enforce Empty Object type (only allows the assignment of empty objects)
- Scoped instances (create tree structures)

## Installation
```bash
npm i ts-lib-extended
```
or
```bash
yarn add ts-lib-extended
```

## Events

With events it is possible to subscribe to specific action or change on an instance.
With events it is possible to subscribe to a specific action or change on an instance.
This is inspired by C# and should work in a similar way.

```ts
Expand Down Expand Up @@ -215,12 +212,12 @@ enum NumberEnum {
bruce
}

function doSomethingWithEnum({}: Enumerable<string>): void {
function doSomethingWithEnum(enum_: Enumerable): void {
/** crazy code here */
}

doSomethingWithEnum(NumberEnum); // TS Error - function argument is limited to string enum type
doSomethingWithEnum(MyEnum); // NO error
doSomethingWithEnum(NumberEnum);
doSomethingWithEnum(MyEnum);
```

## Gain keys and values
Expand Down Expand Up @@ -272,3 +269,79 @@ class SpecialWithoutParams extends Special {
}
}
```

# Scoping

Extend your classes from ScopedInstanceCore to get quick access to scopes. Scopes allow you to create a tree structure within your class instance. Variants can be used to create custom instances per scope.

```ts
import {
InstanceScopeCore,
ScopedInstanceCore,
type InstanceScope
} from 'ts-lib-extended';

type MyScopeVariants = 'dark' | 'light';

class MyClass extends ScopedInstanceCore<MyClassScope> {
constructor(public readonly user?: string) {
super();
}

protected disposeScope(scope_: MyClassScope): void {
scope_.dispose();
}

protected createScope(scopeId_: PropertyKey): MyClassScope {
return new MyClassScope(scopeId_);
}
}

class MyClassScope
extends InstanceScopeCore<MyClass, MyScopeVariants>
implements InstanceScope<MyClass, MyScopeVariants>
{
public get dark(): MyClass {
return this.getOrCreateInstance('dark');
}

public get light(): MyClass {
return this.getOrCreateInstance('light');
}

protected createInstance(variant_: MyScopeVariants): MyClass {
let user: string;

console.log(this.scopeId, variant_);

if (variant_ === 'dark') {
if (this.scopeId === 'starwars') {
user = 'Anakin Skywalker';
} else {
user = 'Riku';
}
} else {
if (this.scopeId === 'starwars') {
user = 'Luke Skywalker';
} else {
user = 'Sora';
}
}

return new MyClass(user);
}

protected disposeInstance(instance_: MyClass): void {
instance_.dispose();
}
}

const mc = new MyClass();
const starwarsScope = mc.scope('starwars');
starwarsScope.dark.user; // => Anakin Skywalker
starwarsScope.light.user; // => Luke Skywalker

const kingdomheartsScope = mc.scope('kingdomhearts'); // or any other scope id
kingdomheartsScope.dark.user; // => Riku
kingdomheartsScope.light.user; // => Sora
```
2 changes: 1 addition & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default [
'@typescript-eslint/no-empty-interface': 'off',
'@typescript-eslint/no-empty-function': 'off',
'@typescript-eslint/no-var-requires': 'warn',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-explicit-any': ['warn', { ignoreRestArgs: true }],
'@typescript-eslint/no-shadow': 'warn',
'@typescript-eslint/no-unsafe-assignment': 'warn',
'@typescript-eslint/no-unsafe-return': 'warn',
Expand Down
Loading

0 comments on commit 06f89d0

Please sign in to comment.