-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
type nothing
emitted in generated .d.ts files
#8345
Comments
I'm tempted to say just don't do that. But I won't. 😉 I definitely don't think the solution is to make |
I get why |
Can it return |
@DanielRosenwasser There are a couple of important differences between
I do agree we have bit of a profusion of nothingness types (undefined, null, void, nothing), but they all have important differences. If anything had to change, we could possibly consider removing the |
here is a less artificial example: function bar() {
var x: string | undefined;
if (x) {
return x;
}
throw new Error();
} |
so for .d.ts purposes, should we 1. write void, 2. undefined, 3. |
(1) and (2) aren't correct and (3) really doesn't help anyone. I think we should (4) issue an error. |
Adding some rationale for (4): There's really no situation ever where you want |
function bar() {
throw new Error();
} Under these reasoning this should produce error too. |
no that is |
Exactly, and that's the same code as in your example (sans no-op if). Do we end-users of the compiler need this level of precision? By all means it may add value internally, but best reduce peculiarity on the outside, coercing |
|
The TypeScript compiler is a TypeScript program so I do not think that would be either possible or valuable. 👍 for making it an error. |
@mhegazy unless this detection generates false positives, as @ahejlsberg pointed out above. function validateType(val: number | string) {
if (typeof val === 'string') return null;
else {
if (typeof val === 'number') return null;
else return val; // validation fails at runtime, but this code is valid
}
}
function validateTypeMoreDetails(val: number | string) {
if (typeof val === 'string') return null;
else {
if (typeof val === 'number') return null;
else return 'Incorrect type ' + typeof val+' ('+val+')'; // should 'nothing' support plus operator with string?
}
} |
Set theory (empowered by union and intersection types) is bound to produce oddities -- hence a need to limit that power in places where it becomes too exotic and impractical. Coercing |
I don't think the |
A function that always throws an exception, never returning normally, logically has a return value of function stub() { throw Error("Unimplemented"); }
interface SomeInterface {
someMethod(): number;
someIrrelevantMethod1(): number;
someIrrelevantMethod2(): string;
}
var obj: SomeInterface = {
someMethod() { ... },
someIrrelevantMethod1: stub,
someIrrelevantMethod2: stub
}; Currently this is an error, despite being perfectly type-safe - making |
@jeffreymorlan perfectly typesafe, but known to produce an error when invoked. |
should be fixed by #8652 |
Fixed in #8652. |
generates invalid .d.ts file
this is a regression introduced by #8340
The text was updated successfully, but these errors were encountered: