Last active
August 27, 2024 17:40
-
-
Save jldec/853dc7c56df368b233c813290f2a963d to your computer and use it in GitHub Desktop.
Typescript surprises
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 checkerhttps://www.typescriptlang.org/docs/handbook/2/functions.html#return-type-void