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

Using this-type with intersection type makes it possible to simulate HKT #40928

Closed
ENvironmentSet opened this issue Oct 3, 2020 · 4 comments
Closed
Labels
Question An issue which isn't directly actionable in code

Comments

@ENvironmentSet
Copy link

ENvironmentSet commented Oct 3, 2020

TypeScript Version: Lastly tested in v4.1.0-dev20201003, but this has been existing at least since v3.8

Search Terms: this-type, this type, intersection, HKT, higher kinded type

Code

export interface HKT {
  param: unknown;
  result: unknown;
}

interface NotHKT extends HKT {
  result: this['param'] extends true ? false : true;
}

interface FstHKT extends HKT {
  result: this['param'] extends [infer A, infer _] ? A : never;
}

interface ArrayHKT extends HKT {
  result: Array<this['param']>;
}

export type Apply<f extends HKT, x>
  = (f & { param: x })['result'];

type Test1 = Apply<NotHKT, true> // be deducedd to `false`
type Test2 = Apply<FstHKT, [string, boolean]> // be deduced to `string`
type Test3 = Apply<ArrayHKT, number> // be deduced to `number[]`

Expected behavior: I expected that reduction of this type doesn't depends on type that is intersected with.

Actual behavior: reduction of this type depends on type that is intersected with.

The actual behavior makes sense to me(at least). It's not hard to accept its behavior. However, It looks like unintended behavior or bug (just like recursive conditional type before) so that I can't certain it's okay to use this. Is this idea safe to use?

Playground Link

@ENvironmentSet ENvironmentSet changed the title Using this-type with intersection type make it possible to simulate HKT Using this-type with intersection type makes it possible to simulate HKT Oct 3, 2020
@jcalz
Copy link
Contributor

jcalz commented Oct 4, 2020

Related to #1213

@andrewbranch
Copy link
Member

Simpler version of the question:

interface A {
  a: string;
  getA: this['a'];
}

// Should T be 'never' or 'string'?
type T = (A & { a: number })['getA'];

Is this idea safe to use?

I don’t really know the answer to this question. I can’t find an instance of this in our test suite, so perhaps in some trivial way, the answer is no. But I would not be surprised if this behavior was intentional. It looks like it could be important for some mixin patterns, for example.

@andrewbranch
Copy link
Member

@DanielRosenwasser agrees that this is probably intentional 😄

@andrewbranch andrewbranch added the Question An issue which isn't directly actionable in code label Oct 6, 2020
@ENvironmentSet
Copy link
Author

Thx for the work :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants