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

conditional generic Type assignable Error #47127

Closed
iahu opened this issue Dec 13, 2021 · 5 comments
Closed

conditional generic Type assignable Error #47127

iahu opened this issue Dec 13, 2021 · 5 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@iahu
Copy link

iahu commented Dec 13, 2021

Bug Report

πŸ”Ž Search Terms

conditional

πŸ•— Version & Regression Information

  • This changed between versions 4.4.4 and 4.5.2

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type KonvaEventObject<T> = {

}

export type GetMultiType<T extends string> = T extends `${infer A} ${infer B}`
  ? GetMultiType<A> | GetMultiType<B>
  : T extends `${infer C}.${string}`
  ? GetMultiType<C>
  : T extends keyof DocumentEventMap
  ? KonvaEventObject<DocumentEventMap[T]>
  : T extends 'tap' | 'touchstart' | 'touchmove' | 'touchcancel' | 'touchend'
  ? KonvaEventObject<TouchEvent>
  : KonvaEventObject<Event>


type Handler  = (e: KonvaEventObject<any>) => void
const on = (type: string, handler: Handler) => {

}
const off = (type: string, handler: Handler) => {

}

export const KAD_ACTION_NS = 'kadAction'
export const withKadActionNS = (s: string) => (s.includes('.') ? s : `${s}.${KAD_ACTION_NS}`)
export const ensureWithNS = (type: string) => type.split(' ').map(withKadActionNS).join(' ')
export const onceOn = <T extends string>(target: Node, type: T, lisenter: (event: GetMultiType<T>) => void) => {
  const typeWithNS = ensureWithNS(type)
  const handler = (event: GetMultiType<T>) => {
    lisenter(event)
    // throw an error
    off(typeWithNS, handler)
  }
  // also throw an error here
  return on(typeWithNS, handler)
}

πŸ™ Actual behavior

Type 'KonvaEventObject' is not assignable to type 'GetMultiType'

πŸ™‚ Expected behavior

can assign type 'KonvaEventObject<NodeEventMap[T]>' to type 'GetMultiType'

@RyanCavanaugh
Copy link
Member

Bisect to #46429

@RyanCavanaugh
Copy link
Member

RyanCavanaugh commented Dec 13, 2021

Simplified

type EventWrapper<T> = {
  event: T;
}

export type GetEventType<T extends string> =
  T extends `${infer A} ${string}` ?
    GetEventType<A>
    : T extends keyof DocumentEventMap ?
      EventWrapper<DocumentEventMap[T]>
    : EventWrapper<Event>

function go<T extends string>(type: T, lisenter: (event: GetEventType<T>) => void) {
  const src: EventWrapper<any> = null as any;
  // OK in 4.4, error in 4.5
  // Should be OK [?]; all terminuses of GetEventType<T> are EventWrapper<any>'s
  const dst: GetEventType<T> = src;
}

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Dec 13, 2021
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.6.0 milestone Dec 13, 2021
@RyanCavanaugh RyanCavanaugh removed the Needs Investigation This issue needs a team member to investigate its status. label Dec 13, 2021
@RyanCavanaugh RyanCavanaugh removed this from the TypeScript 4.6.0 milestone Dec 13, 2021
@RyanCavanaugh
Copy link
Member

@RyanCavanaugh
Copy link
Member

FYI @ahejlsberg for visibility anyway

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Dec 14, 2021
@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants