Hacker News
new
|
past
|
comments
|
ask
|
show
|
jobs
|
submit
login
pverheggen
5 months ago
|
parent
|
context
|
favorite
| on:
Unconventional Ways to Cast in TypeScript
Why not use annotation instead?
const props: ['foo'] = ['foo']
fenomas
5 months ago
|
next
[–]
Didn't occur to me, that's certainly more defensible! Though maybe less humorous.
halflife
5 months ago
|
prev
[–]
Or: cons foo = [‘foo’] as const;
wk_end
5 months ago
|
parent
|
next
[–]
> In a case like that `as const` doesn't work, since the function doesn't expect a readonly argument.
halflife
5 months ago
|
root
|
parent
|
next
[–]
Ah, missed that. Sorry.
cat-whisperer
5 months ago
|
parent
|
prev
[–]
I don’t get this? why do I need to say as const?
afdbcreid
5 months ago
|
root
|
parent
|
next
[–]
`as const` is a special annotation that lets the TypeScript compiler infers the more specific type `["foo"]` instead of `string[]`.
halflife
5 months ago
|
root
|
parent
|
prev
[–]
As const creates a typed tuple instead of a typed array
Guidelines
|
FAQ
|
Lists
|
API
|
Security
|
Legal
|
Apply to YC
|
Contact
Search: