Skip to content

Instantly share code, notes, and snippets.

@jldec
Last active August 27, 2024 17:40
Show Gist options
  • Save jldec/853dc7c56df368b233c813290f2a963d to your computer and use it in GitHub Desktop.
Save jldec/853dc7c56df368b233c813290f2a963d to your computer and use it in GitHub Desktop.
Typescript surprises
// 1.
type FunctionType = {
(): void
function_prop: string
}
// 2.
type ObjectType = {
fn(): void
object_prop: string
}
// 3.
const func:FunctionType = () => 1
func.function_prop = 'string prop for func'
const obj:ObjectType = {
fn: func,
object_prop: 'string prop for obj'
}
@jldec
Copy link
Author

jldec commented Aug 26, 2024

I've been working with TypeScript for a while, and I still run into surprises - Here are 3 that distracted me today.
TS playground

1. curly braces for a function-with-properties declaration

E.g. https://github.com/honojs/hono/blob/main/src/jsx/base.ts#L17-L21
On first read, this looked very much like an object type declaration (to me).

https://www.typescriptlang.org/docs/handbook/2/functions.html#call-signatures
Why are these objects called "call signutures" in the docs?
Why is there a : in front of the return value instead of =>

2. semicolons (or nothing) to separate properties in an object type declaration

https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#object-types

3. void function declarations are not enforced by the type checker

https://www.typescriptlang.org/docs/handbook/2/functions.html#return-type-void

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment