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

[Feature Request] Tagged typing for Expectation results #46

Open
ElijahKotyluk opened this issue Jan 12, 2022 · 2 comments
Open

[Feature Request] Tagged typing for Expectation results #46

ElijahKotyluk opened this issue Jan 12, 2022 · 2 comments
Assignees
Labels
enhancement New feature or request @onyx/matchers Matchers package types

Comments

@ElijahKotyluk
Copy link
Member

ElijahKotyluk commented Jan 12, 2022

Proposed Feature

Tagged types for Expectations

Motivation

Type safety

Example

export class ExpectError extends Error {
  public constructor(message: string) {
    super(message)
    this.name = 'ExpectError'
  }
}

interface AnyMatchers {
  [key: string]: (expectation: any, ...args: any[]) => boolean
}

type OmitFirstArg<A> = A extends (x: any, ...args: infer P) => infer R ? (...args: P) => R : never

type Expectations<M extends AnyMatchers> = { [K in keyof M]: OmitFirstArg<M[K]> }

type NegatedExpectations<M extends AnyMatchers> = Expectations<M> & {
  not: Expectations<M>
}


type ExpectResult<A, E> = {
  matcher: string
  error?: ExpectError
  status: ExpectStatus
  actual: A
  expected: E
}

enum ExpectStatus {
  PASS = 'Pass',
  FAIL = 'Fail',
}

const _expectFail = <F, P = never> (fail: F): ExpectationResult<F, P> => ({ _status: ExpectStatus.FAIL, fail })
const _expectPass = <P, F = never> (pass: P): ExpectationResult<F, P> => ({ _status: ExpectStatus.PASS, pass })

const expectPass: <F = never, P = never> (pass: P) => ExpectationResult<F, P> = _expectPass
const expectFail: <F = never, P = never> (fail: F) => ExpectationResult<F, P> = _expectFail

interface IExpectFail<F> {
  readonly _status: ExpectStatus.FAIL
  readonly fail: F
}

interface IExpectPass<P> {
  readonly _status: ExpectStatus.PASS
  readonly pass: P
}

type ExpectationResult<F, P> = IExpectFail<F> | IExpectPass<P>

@ElijahKotyluk ElijahKotyluk added the enhancement New feature or request label Jan 12, 2022
@ElijahKotyluk
Copy link
Member Author

@sh7dm let me know if you have any opinions when you have time.

@ElijahKotyluk ElijahKotyluk self-assigned this Jan 12, 2022
@ElijahKotyluk ElijahKotyluk changed the title [Feature Request] [Feature Request] Tagged typing for Expectation results Jan 12, 2022
@ElijahKotyluk ElijahKotyluk added the @onyx/matchers Matchers package label Jan 12, 2022
@dsseng
Copy link
Member

dsseng commented Jan 14, 2022

Well, type-safe design should look perfectly here. Key Onyx feature is being based on TypeScript.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request @onyx/matchers Matchers package types
Projects
None yet
Development

No branches or pull requests

2 participants