Skip to content

Commit

Permalink
chore: organize types layout
Browse files Browse the repository at this point in the history
  • Loading branch information
ashgw committed Apr 27, 2024
1 parent 5155461 commit 56becf5
Showing 1 changed file with 37 additions and 21 deletions.
58 changes: 37 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Collection of utility types and decorators to bullet proof TypeScript even more.
<hr/>
</div>

### Installation
## Installation
**npm**
```bash
npm i ts-roids
Expand All @@ -31,36 +31,53 @@ And if you're using decorators, set this property inside `tsconfig.json`.
}
```
Requires TypesScript `v5.0`+
### Documentation
## Documentation
Checkout the [API reference](https://ashgw.github.io/ts-roids/) for all usage examples with details.
#### Here are all the types

#### Example using the decorators
This is how one might use the decorators:
### All the types
**TODO**
### Example using the decorators
#### Finalize and [freeze](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze) objects

```ts
import type { Optional, NewType } from 'ts-roids';
import type { Optional, NewType, MaybeUndefined } from 'ts-roids';
import { Final, Frozen } from 'ts-roids';

type Bar = NewType<'Bar',string>;
type Bar = NewType<'Bar', string>;
type Baz = NewType<'Baz', string>;
type Secret = NewType<'Secret', string>;

abstract class BaseFoo<T> {
abstract someFoo(): T;
protected abstract get foo(): T;
abstract requestFoo(secret: Secret, baz: Baz): Optional<T>;
}

@Final
@Frozen
class Foo<T> extends BaseFoo<T> {
foo: T;
private readonly _foo: T;
bar: Optional<Bar>;

constructor(foo: T, bar?: MaybeUndefined<Bar>) {
super();
this.foo = foo;
this._foo = foo;
this.bar = bar ?? null;
}
override someFoo(): T {
return this.foo;
protected override get foo(): T {
console.log('do some stuff first');
return this._foo;
}

requestFoo(secret: Secret, baz: Baz): Optional<T> {
// A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value
if (
secret.concat().toLowerCase() === '123' &&
baz.concat().toLowerCase() === 'baz' &&
this.bar !== null
) {
return this.foo;
}

return null; // So you have to explicitly return null here.
}
}

Expand All @@ -73,20 +90,20 @@ class SubFoo extends Foo<string> {
// No problem with instantiation
const foo = new Foo<string>('foo');

// Since the object is final:
// Since the object is final:

// The line below will cause a TypeError: Cannot inherit from the finl class Foo
const sub = new SubFoo('subFoo');
const _ = new SubFoo('subFoo');

// Since the object is frozen:
// Since the object is frozen:

// The line below will cause a TypeError: Cannot add property 'someFoo', object is not extensible
foo.someFoo = () => {
// The line below will cause a TypeError: Cannot add property 'requestFoo', object is not extensible
foo.requestFoo = () => {
return 'not foo';
};

// The line below will cause a TypeError: Cannot assign to read only property 'bar'
foo.bar = 'not bar' as Bar;
foo.bar = 'not bar' as Bar;
```
You can also [seal](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/seal) an object.
```ts
Expand All @@ -113,7 +130,6 @@ Speaking of `final`, The TypeScript team has not yet introduced a built-in final
Although they introduced `override` in [`v4.3`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-3.html#override-and-the---noimplicitoverride-flag) .

Decorators like ``@Final`` provide a limited way to emulate final behavior, these are merely band-aids for now, until TS officially supports a true final modifier.
````

## Changelog

Expand All @@ -124,5 +140,5 @@ See [releases](https://github.com/ashgw/ts-roids/releases).

Pull requests are always welcome, but it's preferable not to submit a bare PR. It's best when a PR addresses a specific issue. Therefore, for bugs, documentation, or feature requests, consider submitting [an issue](https://github.com/AshGw/ts-roids/issues/new/choose) first.

### License
## License
[GPL-3](/LICENSE)

0 comments on commit 56becf5

Please sign in to comment.