0

I like to use typescript interfaces for function properties, But I like to use default properties as well. How can I use both?

export interface test_interface{
   owner: string, 
   length?: number, 
   max_players?: number,
}
const test_function = (test_props: test_interface) => {
    const x = test_props.length; //could be undefined and I want a default value
}

1 Answer 1

1

You can accomplish this with destructuring in the function declaration.

export interface test_interface{
   owner: string, 
   length?: number, 
   max_players?: number,
}
const test_function = ({owner, length = 5, max_players = 4}: test_interface) => {
    const x = test_props.length; //length is 5 or whatever was passed in on the function call
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.