microsoft / microsoft/TypeScript

multi return type declaration emit not as expected

Open
#35,775 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Needs Investigation
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 3.8.0-dev.20191213

Search Terms:

Code

origin code

export const enum EnumParseInputUrl
{
	UNKNOWN,
	STRING,
	URL,
	URLSEARCHPARAMS,
}

export function parseInputUrl<T extends string | number | URL | URLSearchParams>(_input: T)
{
	if (typeof _input === 'number')
	{
		let value = _input.toString();

		return {
			type: EnumParseInputUrl.STRING,
			_input,
			value,
		}
	}
	else if (typeof _input === 'string')
	{
		let value = _input.toString();

		return {
			type: EnumParseInputUrl.STRING,
			_input,
			value,
		}
	}
	else if (_input instanceof URL)
	{
		let value = _input;

		return {
			type: EnumParseInputUrl.URL,
			_input,
			value,
		}
	}
	else if (_input instanceof URLSearchParams)
	{
		let value = _input;

		return {
			type: EnumParseInputUrl.URLSEARCHPARAMS,
			_input,
			value,
		}
	}

	let value = _input.toString();

	return {
		type: EnumParseInputUrl.UNKNOWN,
		_input,
		value,
	}
}

the code of if i want export as expected

export function parseInputUrl<T extends string | number | URL | URLSearchParams>(_input: T)
{
	if (typeof _input === 'number')
	{
		let value = _input.toString();

		return {
			type: EnumParseInputUrl.STRING as const,
			_input,
			value,
		}
	}
	else if (typeof _input === 'string')
	{
		let value = _input.toString();

		return {
			type: EnumParseInputUrl.STRING as const,
			_input,
			value,
		}
	}
	else if (_input instanceof URL)
	{
		let value = _input;

		return {
			type: EnumParseInputUrl.URL as const,
			_input,
			value,
		}
	}
	else if (_input instanceof URLSearchParams)
	{
		let value = _input;

		return {
			type: EnumParseInputUrl.URLSEARCHPARAMS as const,
			_input,
			value,
		}
	}

	let value = _input.toString();

	return {
		type: EnumParseInputUrl.UNKNOWN as const,
		_input,
		value,
	}
}

Expected behavior:

export declare function parseInputUrl<T extends string | number | URL | URLSearchParams>(_input: T): {
    type: EnumParseInputUrl.STRING;
    _input: T & number;
    value: string;
} | {
    type: EnumParseInputUrl.STRING;
    _input: T & string;
    value: string;
} | {
    type: EnumParseInputUrl.URL;
    _input: T & URL;
    value: T & URL;
} | {
    type: EnumParseInputUrl.URLSEARCHPARAMS;
    _input: T & URLSearchParams;
    value: T & URLSearchParams;
} | {
    type: EnumParseInputUrl.UNKNOWN;
    _input: T;
    value: string;
};

Actual behavior:

export declare const enum EnumParseInputUrl {
    UNKNOWN = 0,
    STRING = 1,
    URL = 2,
    URLSEARCHPARAMS = 3
}
export declare function parseInputUrl<T extends string | number | URL | URLSearchParams>(_input: T): {
    type: EnumParseInputUrl;
    _input: T & URL;
    value: T & URL;
} | {
    type: EnumParseInputUrl;
    _input: T & URLSearchParams;
    value: T & URLSearchParams;
} | {
    type: EnumParseInputUrl;
    _input: T;
    value: string;
};

Playground Link:

Related Issues:

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by compiling the supplied TypeScript reproduction with declaration emit using the reported 3.8.0-dev.20191213 version and compare the actual declaration with the expected output. Trace the compiler path responsible for inferring and emitting the function's return type; done means the emitted declaration preserves the distinct enum members and narrowed intersections shown in the expected behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.