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

Unclear compiler error with map function and number or types #13594

Closed
dbaeumer opened this issue Jan 20, 2017 · 4 comments
Closed

Unclear compiler error with map function and number or types #13594

dbaeumer opened this issue Jan 20, 2017 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@dbaeumer
Copy link
Member

dbaeumer commented Jan 20, 2017

TypeScript Version: 2.1.5
Code

/**
 * The diagnostic's serverity.
 */
export namespace DiagnosticSeverity {
	/**
	 * Reports an error.
	 */
	export const Error = 1;
	/**
	 * Reports a warning.
	 */
	export const Warning = 2;
	/**
	 * Reports an information.
	 */
	export const Information = 3;
	/**
	 * Reports a hint.
	 */
	export const Hint = 4;
}

export type DiagnosticSeverity = 1 | 2 | 3 | 4;

/**
 * Represents a diagnostic, such as a compiler error or warning. Diagnostic objects
 * are only valid in the scope of a resource.
 */
export interface Diagnostic {
	/**
	 * The diagnostic's severity. Can be omitted. If omitted it is up to the
	 * client to interpret diagnostics as error, warning, info or hint.
	 */
	severity?: DiagnosticSeverity;

	/**
	 * The diagnostic's code. Can be omitted.
	 */
	code?: number | string;

	/**
	 * A human-readable string describing the source of this
	 * diagnostic, e.g. 'typescript' or 'super lint'.
	 */
	source?: string;

	/**
	 * The diagnostic's message.
	 */
	message: string;
}

function bug(): Diagnostic[] {
	let values: any[];
	return values.map((value) => {
		return {
			severity: DiagnosticSeverity.Error,
			message: 'message'
		}
	});
}

Expected behavior:

No compile error

Interestingly the error disappears if the fat arrow function gets typed:

function bug(): Diagnostic[] {
	let values: any[];
	return values.map((value): Diagnostic => {
		return {
			severity: DiagnosticSeverity.Error,
			message: 'message'
		}
	});
}
@dbaeumer
Copy link
Member Author

Another possible workaround is:

export namespace DiagnosticSeverity {
	/**
	 * Reports an error.
	 */
	export const Error: 1 = 1;
	/**
	 * Reports a warning.
	 */
	export const Warning: 2 = 2;
	/**
	 * Reports an information.
	 */
	export const Information: 3 = 3;
	/**
	 * Reports a hint.
	 */
	export const Hint: 4 = 4;
}

@dbaeumer
Copy link
Member Author

The typing of the const doesn't work for d.ts file generation since the generated d.ts again looks like this:

export declare namespace DiagnosticSeverity {
    /**
     * Reports an error.
     */
    const Error: 1;
    /**
     * Reports a warning.
     */
    const Warning: 2;
    /**
     * Reports an information.
     */
    const Information: 3;
    /**
     * Reports a hint.
     */
    const Hint: 4;
}

@RyanCavanaugh
Copy link
Member

const One = 1; // Should behave the same as ExplicitOne
const ExplicitOne: 1 = 1;
type OneOrTwo = 1 | 2;
type Thing = { x: OneOrTwo; }

// Error
let x: Thing = (() => ({ x: One }))();
// OK
let y: Thing = (() => ({ x: ExplicitOne }))();

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Jan 20, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Jan 21, 2017

@RyanCavanaugh's example is by design. the two do not behave the same, one is fresh (One), which gets widened to the base type in mutable locations, where the other ExplicitOne does not, since it had an explicit type.

The above issue is really a consequence of not using the return type position as an inference location, see #11152. The only place the return type of map is inferred from is the return type, and there is no contextual type there, so you get the widened type instead of the literal one. #11152 tracks that.

@mhegazy mhegazy added Duplicate An existing issue was already created and removed Bug A bug in TypeScript labels Jan 21, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants