Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Function signature confusion after ternary operator #35707

Copy link
Copy link
@Mickael-van-der-Beek

Description

@Mickael-van-der-Beek
Issue body actions

I'm expecting this to not be a bug but I can't seem to figure out what else it could be.
Basically I'm trying to create a (TLS)Socket instance using the core Node.js net or tls modules depending on a condition.
Instantiating them by directly referencing the module works fine but whenever I introduce a level of indirection by conditionally defining the TCP client module, compilation breaks.

Code

import net from 'net';
import tls from 'tls';

const socketA = net.connect({ host: 'localhost', port: 1234 });
const socketB = tls.connect({ host: 'localhost', port: 1234 });

const protocol = process.argv[2];
const client = protocol === 'https' ? tls : net;
const socketC: net.Socket | tls.TLSSocket = client.connect({ host: 'localhost', port: 1234 });

console.log(socketA, socketB, socketC);

Expected behavior:

Successful compilation.

Actual behavior:

This expression is not callable.
  Each member of the union type
    '{ (options: NetConnectOpts, connectionListener?: (() => void) | undefined): Socket;
     (port: number, host?: string | undefined, connectionListener?: (() => void) | undefined): Socket; 
     (path: string, connectionListener?: (() => void) | undefined): Socket; } | { ...; }'
has signatures, but none of those signatures are compatible with each other.

12 const socketC = client.connect({ host: 'localhost', port: 443 });
                          ~~~~~~~

Playground Link:

Playground Link

Related Issues:

Reactions are currently unavailable

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.