Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Class utility #56

Merged
merged 2 commits into from
Jan 14, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ This gives you the power to prioritize our work and support project contributors
* [`$Call<T>`](#callt)
* [`$Shape<T>`](#shapet)
* [`$NonMaybeType<T>`](#nonmaybetypet)
* [`Class<T>`](#classt)

## Deprecated API (use at own risk)
* `getReturnOfExpression()` - from TS v2.0 it's better to use type-level `ReturnType` instead
Expand Down Expand Up @@ -707,6 +708,26 @@ type Name = $NonMaybeType<MaybeName>;

[⇧ back to top](#flows-utility-types)

### `Class<T>`

Given a type T representing instances of a class C, the type Class<T> is the type of the class C
https://flow.org/en/docs/types/utilities/#toc-class
\* Differs from original Flow's util - implements only constructor part and won't include any static members. Additionally classes in Typescript are not treated as nominal

**Usage:**

```ts
import { Class } from 'utility-types';

class Store {}

function makeStore(storeClass: Class<Store>) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also include return type Store here, so it is more obvious for the user what it should be

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After that small change I'll merge

return new storeClass();
}
```

[⇧ back to top](#flows-utility-types)

---

MIT License
Expand Down
2 changes: 2 additions & 0 deletions src/__snapshots__/utility-types.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ exports[`$ReadOnly testType<$ReadOnly<Props>>() 1`] = `"_DeepReadonlyObject<Prop
exports[`$Shape testType<$Shape<Props>>() 1`] = `"Partial<Props>"`;

exports[`$Values testType<$Values<Props>>() 1`] = `"string | number | boolean"`;

exports[`Class testType<Class<Foo>>() 1`] = `"Class<Foo>"`;
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export {
$ElementType,
$Shape,
$NonMaybeType,
Class,
} from './utility-types';

export {
Expand Down
9 changes: 9 additions & 0 deletions src/utility-types.spec.snap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
$ElementType,
$Shape,
$NonMaybeType,
Class,
} from './utility-types';
import { _DeepReadonlyObject } from './mapped-types';
/**
Expand All @@ -18,6 +19,8 @@ import { _DeepReadonlyObject } from './mapped-types';
type Props = { name: string; age: number; visible: boolean };
type DefaultProps = { age: number };

class Foo {}

/**
* Tests
*/
Expand Down Expand Up @@ -104,3 +107,9 @@ it('$NonMaybeType', () => {
// @dts-jest:pass:snap -> string
testType<$NonMaybeType<string|null|undefined>>();
});

// @dts-jest:group Class
it('Class', () => {
// @dts-jest:pass:snap -> Class<Foo>
testType<Class<Foo>>();
});
9 changes: 9 additions & 0 deletions src/utility-types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
$ElementType,
$Shape,
$NonMaybeType,
Class,
} from './utility-types';
import { _DeepReadonlyObject } from './mapped-types';
/**
Expand All @@ -18,6 +19,8 @@ import { _DeepReadonlyObject } from './mapped-types';
type Props = { name: string; age: number; visible: boolean };
type DefaultProps = { age: number };

class Foo {}

/**
* Tests
*/
Expand Down Expand Up @@ -104,3 +107,9 @@ it('$NonMaybeType', () => {
// @dts-jest:pass:snap
testType<$NonMaybeType<string|null|undefined>>();
});

// @dts-jest:group Class
it('Class', () => {
// @dts-jest:pass:snap
testType<Class<Foo>>();
});
7 changes: 7 additions & 0 deletions src/utility-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,10 @@ export type $Shape<T extends object> = Partial<T>;
* @see https://flow.org/en/docs/types/utilities/#toc-nonmaybe
*/
export type $NonMaybeType<T> = NonNullable<T>;

/**
* Class
* @desc Represents constructor of type T
* @see https://flow.org/en/docs/types/utilities/#toc-class
*/
export type Class<T> = new (...args: any[]) => T;