Skip to content

Throw types - #40402

Closed
Jack Works (Jack-Works) wants to merge 4 commits into
microsoft:templateTypesfrom
Jack-Works:throwTypes
Closed

Throw types#40402
Jack Works (Jack-Works) wants to merge 4 commits into
microsoft:templateTypesfrom
Jack-Works:throwTypes

Conversation

@Jack-Works

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

Copy link
Copy Markdown
Contributor

Please move to #40468

@alitaheri

Copy link
Copy Markdown

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
Copy Markdown
Contributor

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
Copy Markdown
Contributor Author

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

@mmkal

Copy link
Copy Markdown

Jack Works (@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
Copy Markdown
Contributor Author

Wenlu Wang (@Kingwl) hi can you let the bot to pack this?

@Kingwl

Copy link
Copy Markdown
Contributor

sure. TypeScript Bot (@typescript-bot) pack this

@typescript-bot

TypeScript Bot (typescript-bot) commented Sep 7, 2020

Copy link
Copy Markdown
Contributor

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

@Jack-Works

Jack Works (Jack-Works) commented Sep 8, 2020

Copy link
Copy Markdown
Contributor Author

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 Wenlu Wang (@Kingwl) can you call the bot again?

@Kingwl

Wenlu Wang (Kingwl) commented Sep 8, 2020

Copy link
Copy Markdown
Contributor

As your wish. :XD

TypeScript Bot (@typescript-bot) pack this.

@typescript-bot

TypeScript Bot (typescript-bot) commented Sep 8, 2020

Copy link
Copy Markdown
Contributor

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

@typescript-bot

TypeScript Bot (typescript-bot) commented Sep 8, 2020

Copy link
Copy Markdown
Contributor

Hey Wenlu Wang (@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

Orta Therox (orta) commented Sep 8, 2020

Copy link
Copy Markdown
Contributor

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 (@typescript-bot) pack this.

@orta

Copy link
Copy Markdown
Contributor

Third time lucky ( see #40445 )

TypeScript Bot (@typescript-bot) pack this.

@typescript-bot

TypeScript Bot (typescript-bot) commented Sep 9, 2020

Copy link
Copy Markdown
Contributor

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

@typescript-bot

TypeScript Bot (typescript-bot) commented Sep 9, 2020

Copy link
Copy Markdown
Contributor

Hey Orta Therox (@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
Ryan Cavanaugh (RyanCavanaugh) marked this pull request as draft September 9, 2020 17:41
@mmkal

Copy link
Copy Markdown

Jack Works (@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
Copy Markdown
Contributor Author

Thanks Misha Kaletsky (@mmkal) , I'll try to investigate the bug later

@Jack-Works

Copy link
Copy Markdown
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.

@Jack-Works

Jack Works (Jack-Works) commented Sep 10, 2020

Copy link
Copy Markdown
Contributor Author

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

@Jack-Works

Copy link
Copy Markdown
Contributor Author

Misha Kaletsky (@mmkal) the bug has been fixed!

@microsoft Microsoft (microsoft) locked as resolved and limited conversation to collaborators Oct 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

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.

8 participants