Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use unknown type instead of any for Typescript definitions of response types #3933

Closed
AsazuTaiga opened this issue Aug 4, 2021 · 2 comments

Comments

@AsazuTaiga
Copy link

AsazuTaiga commented Aug 4, 2021

Is your feature request related to a problem? Please describe.

I strongly sympathize with issue #3313, which was closed by BOT, and feel that it is an issue worth considering, so I will raise another issue with the same content.

Describe the solution you'd like

I think that the type argument of the response should be changed from <T = any> to <T = unknown>.

// <T = any>
const asyncFunction = async () => {
  const response = await axios.get('/path/to/endpoint')
  const data = response.data // any 😢
  const foo = data.foo // not type error, but foo may be undefined
  // ...
}
// if <T = unknown>
const asyncFunction = async () => {
  const response = await axios.get('/path/to/endpoint')
  const data = response.data // unknown
  // const foo= data.foo // type error
  if (hasFoo(data)) { // type testing is compulsory 😄 
    const foo = data.foo
    // ...
  }
}

Describe alternatives you've considered

This may be breaking changes, so it might be a good idea to just document the following wrappers.

async function typeSafeGet<T = unknown>(
  url: string,
  config?: AxiosRequestConfig,
) {
  return axios.get<T>(url, config)
}

Additional context

In #3002, @carloschida agrees that unknown is optimal choice.

@carloschida
Copy link
Contributor

I also proposed a change in #2995.

@AsazuTaiga
Copy link
Author

Thank you very much.
I wasn't aware of the PR on your side, sorry about that.
I think this issue is no longer needed, so I'll close it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants