OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Why does TypeScript Type Inference Fail with Unimplemented Interface Method? [duplicate]

  • Thread starter Thread starter Monarch Wadia
  • Start date Start date
M

Monarch Wadia

Guest
I have two TypeScript examples where I'm trying to ensure that a parameter type (Param) is inferred as string.

In the first example, it is inferred as string as expected. But in the second, it is inferred as unknown.

Here are the details:

Working Case​


Code:
class Chat<T> {
    constructor(private adapter: BaseChatAdapter<T>) {}

    send(message: T) {}
}

interface BaseChatAdapter<T> {
    send(message: T): void;
}

class StringAdapter<T = string> implements BaseChatAdapter<T> {
    send(message: T) {}
}

const c = new Chat(new StringAdapter());

type Param = Parameters<typeof c['send']>[0]; // Param is inferred as string

Non-working Case​


Code:
class Chat<T> {
    constructor(private adapter: BaseChatAdapter<T>) {}

    send(message: T) {}
}

interface BaseChatAdapter<T> {
    // The 'send' method is removed
}

class StringAdapter<T = string> implements BaseChatAdapter<T> {
    send(message: T) {}
}

const c = new Chat(new StringAdapter());

type Param = Parameters<typeof c['send']>[0]; // Param is inferred as unknown

In the second example, Param is inferred as unknown, even though I expect it to be string. The only difference between the two cases is the commented-out send method in the BaseChatAdapter interface.

Question:


  1. Why does the absence of the send method in the BaseChatAdapter interface affect the type inference in the second example?


  2. (Removed, see comments)

Any insights or explanations would be greatly appreciated!

<p>I have two TypeScript examples where I'm trying to ensure that a parameter type (Param) is inferred as string.</p>
<p>In the first example, it is inferred as <code>string</code> as expected. But in the second, it is inferred as <code>unknown</code>.</p>
<p>Here are the details:</p>
<h2>Working Case</h2>
<pre><code>class Chat<T> {
constructor(private adapter: BaseChatAdapter<T>) {}

send(message: T) {}
}

interface BaseChatAdapter<T> {
send(message: T): void;
}

class StringAdapter<T = string> implements BaseChatAdapter<T> {
send(message: T) {}
}

const c = new Chat(new StringAdapter());

type Param = Parameters<typeof c['send']>[0]; // Param is inferred as string
</code></pre>
<h2>Non-working Case</h2>
<pre><code>class Chat<T> {
constructor(private adapter: BaseChatAdapter<T>) {}

send(message: T) {}
}

interface BaseChatAdapter<T> {
// The 'send' method is removed
}

class StringAdapter<T = string> implements BaseChatAdapter<T> {
send(message: T) {}
}

const c = new Chat(new StringAdapter());

type Param = Parameters<typeof c['send']>[0]; // Param is inferred as unknown
</code></pre>
<p>In the second example, Param is inferred as <code>unknown</code>, even though I expect it to be string. The only difference between the two cases is the commented-out send method in the BaseChatAdapter interface.</p>
<p>Question:</p>
<ol>
<li><p>Why does the absence of the send method in the BaseChatAdapter interface affect the type inference in the second example?</p>
</li>
<li><p>(Removed, see comments)</p>
</li>
</ol>
<p>Any insights or explanations would be greatly appreciated!</p>
 

Latest posts

Top