Microsoft/TypeScript

【bug】ThisType is invalid when using param on generic object

Open

#52.969 aberto em 25 de fev. de 2023

Ver no GitHub
 (4 comments) (0 reactions) (0 assignees)TypeScript (6.726 forks)batch import
BugDomain: This-TypingHelp Wanted

Métricas do repositório

Stars
 (48.455 stars)
Métricas de merge de PR
 (Mesclagem média 6d 17h) (9 fundiu PRs em 30d)

Description

Bug Report

🔎 Search Terms

ThisType Object Generic

🕗 Version & Regression Information

I was unable to test this on prior versions because I was not using this feature previously

⏯ Playground Link

Playground link with relevant code

💻 Code

type ILifetime = {
  onShareTimeline: () => string
}

function createObject<D>(obj: D & ILifetime & ThisType<D>) {
  console.warn(obj)
}

// working
createObject({
  title: 'demo',
  onShareTimeline() {
    return ''
  }
})

// not working
createObject({
  title: 'demo',
  onShareTimeline() {
    return this.title
  }
})

🙁 Actual behavior

The generic type is broken

🙂 Expected behavior

The generic type should contain the object properties as those are finally included into the result

Guia do colaborador