Typed parameter destructuring #126
Replies: 9 comments 37 replies
-
|
To clarify, you're proposing that JS object literal/destructuring I'm not a fan of redefining JS syntax. It'd be nice to find another way... |
Beta Was this translation helpful? Give feedback.
-
|
For the more complex situation maybe: Another possibility: Just throwing some ideas out there. |
Beta Was this translation helpful? Give feedback.
-
|
More alternatives: Component := ({
(baz: qux)?: boolean = false
})
Component := ({
baz?: boolean: qux = false // The type is always needed, only the alias is optional, so the order is guaranteed
}) |
Beta Was this translation helpful? Give feedback.
-
|
Would it be crazy to consider different symbols than ({x: y}) => y // reassignment as in JS
({: x: number :}) => x // typing(This is somewhat inspired by Flow's Separately, we can add ({: x as y: number :}) => y |
Beta Was this translation helpful? Give feedback.
-
|
So, another alternative that may be more consistent and less prone to invalid usage: Component := ({
foo:: string
...rest:: React.HTMLProps<HTMLElement>
}) =>
// ... |
Beta Was this translation helpful? Give feedback.
-
|
What do you think if:
Id ::= string | number
// 👇 ts
type Id = string | number
User ::=
id:: number
name?: string
// 👇 ts
type User = {
id: number
name?: string
}
Component := ({
foo:: string
bar:: number = 3
baz: qux:: boolean = false
} & React.HTMLProps<HTMLElement>) =>Unfortunately Id := string | number
// 👇 ts
type Id = string | number
User :=
eid:: string
age:? number
// 👇 ts
type User = {
id: number
name?: string
} |
Beta Was this translation helpful? Give feedback.
-
|
Now that I finally understand #126 (reply in thread) (thanks @gustavopch for explaining!), I propose:
So object literals look like this: id := 5
user :=
id:: number
name: 'John': string
age?: number
↓↓↓
const id = 5
const user: {
id: number,
name: string,
age?: number
} = {
id,
name: 'John'
}Destructuring declaration: {id:: number, name: userName: string} := obj
↓↓↓
const {id, name: userName} = obj as {id: number, name: string}
OR
let id: number, userName: string
{id, name: userName} = objDestructuring non-declaration Function: ({id:: number, name: userName: string, age?: number = 18}) => ...
↓↓↓
({id, name: userName, age = 18}: {id: number, name: string, age?: number}) => ... |
Beta Was this translation helpful? Give feedback.
-
|
I propose that we try to keep:
I believe that's just where people expect things to be when they read the code as you can see in all these cases: const name: Type = value
const name: Type = {
name: value
}
const name: Type = (name: Type = defaultValue) => {}
const name = ({
name: newName = defaultValue
}: {
name: Type
}) => {...}So here's how it would look: user :=
name:: string: 'John'
age?: number
↓↓↓
const user: {
name: string
age?: number
} = {
name: 'John',
}
createUser := ({
id: userId:: string
name:: string
age:: number = 0
...rest:: OtherProps
}) =>
// ...
↓↓↓
const createUser = ({
id: userId,
name,
age = 0,
...rest
}: {
id: string
name: string
age: number
} & OtherProps)More generally: ATM, I think this is the best if we want to optimize for readability. |
Beta Was this translation helpful? Give feedback.
-
|
Implemented in #1383! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
What?
Civet:
Output:
Why?
Beta Was this translation helpful? Give feedback.
All reactions