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

Throw types #40402

Closed
wants to merge 4 commits into from
Closed

Throw types #40402

wants to merge 4 commits into from

Conversation

Jack-Works
Copy link
Contributor

@Jack-Works Jack-Works commented Sep 6, 2020

Please move to #40468

@alitaheri
Copy link

This is great, it can help develop and debug complicated type systems much more efficiently. I really hope it lands along with it's base PR on 4.1 😍

@aminpaks
Copy link
Contributor

aminpaks commented Sep 6, 2020

Why would you use typeof keyword in

type MustNumber<T> = T extends number ? T : throw `Expected, but found "${typeof T}"`

T already is a type in that expression.

@Jack-Works
Copy link
Contributor Author

It's can be changed to another keyword if there is a better alternative

@mmkal
Copy link

mmkal commented Sep 7, 2020

@Jack-Works this looks awesome. Is there a playground link we can try? I have a type assertions library (expect-type) and I'd like to see if I can use this to improve the error messages.

@Jack-Works
Copy link
Contributor Author

@Kingwl hi can you let the bot to pack this?

@Kingwl
Copy link
Contributor

Kingwl commented Sep 7, 2020

sure. @typescript-bot pack this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 7, 2020

Heya @Kingwl, I've started to run the tarball bundle task on this PR at 198754f. You can monitor the build here.

@Jack-Works
Copy link
Contributor Author

Jack-Works commented Sep 8, 2020

Oh seems like the bot won't pack this until no test error, I'll handle this later

Now it can pack without error, sorry @Kingwl can you call the bot again?

@Kingwl
Copy link
Contributor

Kingwl commented Sep 8, 2020

As your wish. :XD

@typescript-bot pack this.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 8, 2020

Heya @Kingwl, I've started to run the tarball bundle task on this PR at f1b195d. You can monitor the build here.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 8, 2020

Hey @Kingwl, I've packed this into an installable tgz. You can install it for testing by referencing it in your package.json like so:

{
    "devDependencies": {
        "typescript": "https://typescript.visualstudio.com/cf7ac146-d525-443c-b23c-0d58337efebc/_apis/build/builds/84410/artifacts?artifactName=tgz&fileId=89E67B25A073B99041FC4982F2230A04CEECE579CB68839CB8E82E1316023AB202&fileName=/typescript-4.1.0-insiders.20200908.tgz"
    }
}

and then running npm install.


There is also a playground for this build.

@orta
Copy link
Contributor

orta commented Sep 8, 2020

I'm gonna re-pack, a bunch of PRs got merged on monaco / monaco-ts last night which broke the playground build - I think I fixed it.

@typescript-bot pack this.

@microsoft microsoft deleted a comment from typescript-bot Sep 9, 2020
@microsoft microsoft deleted a comment from typescript-bot Sep 9, 2020
@orta
Copy link
Contributor

orta commented Sep 9, 2020

Third time lucky ( see #40445 )

@typescript-bot pack this.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 9, 2020

Heya @orta, I've started to run the tarball bundle task on this PR at f1b195d. You can monitor the build here.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 9, 2020

Hey @orta, I've packed this into an installable tgz. You can install it for testing by referencing it in your package.json like so:

{
    "devDependencies": {
        "typescript": "https://typescript.visualstudio.com/cf7ac146-d525-443c-b23c-0d58337efebc/_apis/build/builds/84651/artifacts?artifactName=tgz&fileId=F1E73F036BD00B0E703FB5AC26800759E4D8AAA8CB6B9FA6479472D55EF1703002&fileName=/typescript-4.1.0-insiders.20200909.tgz"
    }
}

and then running npm install.


There is also a playground for this build.

@RyanCavanaugh RyanCavanaugh marked this pull request as draft September 9, 2020 17:41
@mmkal
Copy link

mmkal commented Sep 9, 2020

@Jack-Works trying this out on my library now, and it's working beautifully. I did notice one small issue, though. Throw types for generic functions inside generic interfaces can get replaced with Unknown, if the throw type uses both generics. There may be other ways to repro it, but here's a reasonably simple illustrative example:

interface Example<T> {
  tOnly: <U>() => throw `T: ${typeof T}`
  uOnly: <U>() => throw `U: ${typeof U}`
  tAndU: <U>() => throw `T: ${typeof T}, U: ${typeof U}`
}

declare const ex: Example<{foo: string}>

ex.tOnly<{bar: string}>() // Error looks good: Type instantiated results in a throw type saying: T: { foo: string; }

ex.uOnly<{bar: string}>() // Error looks good: Type instantiated results in a throw type saying: U: { bar: string; }

ex.tAndU<{bar: string}>() // Error looks bad: Type instantiated results in a throw type saying: Unknown

playground link

@Jack-Works
Copy link
Contributor Author

Thanks @mmkal , I'll try to investigate the bug later

@Jack-Works
Copy link
Contributor Author

I found another potential usage. This PR might be able to resolve #18433 (Dangerous "name" global).
If we mark it as declare var name: throw "Error-prone deprecated global variable" and emit type error once it referenced, the problem is gone.

@ahejlsberg ahejlsberg closed this Sep 10, 2020
@Jack-Works
Copy link
Contributor Author

Jack-Works commented Sep 10, 2020

hi everyone please move to #40468 cause the base branch has been merged so this PR is closed automatically.

@Jack-Works
Copy link
Contributor Author

@mmkal the bug has been fixed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants