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

Scoped types #56310

Closed
6 tasks done
oliveryasuna opened this issue Nov 4, 2023 · 6 comments
Closed
6 tasks done

Scoped types #56310

oliveryasuna opened this issue Nov 4, 2023 · 6 comments
Labels
Unactionable There isn't something we can do with this issue

Comments

@oliveryasuna
Copy link

πŸ” Search Terms

scope scoped scopes type types

βœ… Viability Checklist

⭐ Suggestion

It would be great if you could scope types. Types could be scoped like everything else with { SCOPE }.

πŸ“ƒ Motivating Example

Code can be written more intentionally. If a type is meant to only be used within a specific function, for example, it should be defined in that function.

type User = {
  id: string
  name: string,
  lastLogin: Date
};

function getUser(id: number): User {
  type UserInDb = {
    id: number,
    name: string,
    lastLogin: number
  };

  const user: UserInDb = fetchUserById(id);

  return {
    id: user.id,
    name: user.name,
    lastLogin: new Date(user.lastLogin)
  };
}

πŸ’» Use Cases

  1. What do you want to use this for?

This would allow developer to write clearer and more intentional code. A scoped type would clearly demonstrate that it is only used within the scope it is defined.

  1. What shortcomings exist with current approaches?

The language does not demonstrate how and where a type is used.

  1. What workarounds are you using in the meantime?

There are no alternatives at the moment.

@fatcerberus
Copy link

I'm confused by the motivating example - surely the return type of fetchUserById is a type that's already accessible to you so defining the same type locally is redundant?

@xiBread
Copy link

xiBread commented Nov 4, 2023

Feel like this has been suggested before... If not then #7061 is related

@jcalz
Copy link
Contributor

jcalz commented Nov 4, 2023

I'm confused. The function-local type works just fine. The only problems with this code seem to be that you have not declared fetchUserById(), and you're trying to assign a number to a string for some reason. What, specifically, is the problem we're trying to solve here?

@oliveryasuna
Copy link
Author

I'm confused. The function-local type works just fine. The only problems with this code seem to be that you have not declared fetchUserById(), and you're trying to assign a number to a string for some reason. What, specifically, is the problem we're trying to solve here?

I suppose my example was a poor one. Though, we often write functions that use intermediate types different than the return type. Right now, you have to define those types on a module's top-level scope. Even if they are not exported, they are visible to the entire module. I am proposing the ability to define types with deeper scopes, such as a function's scope. This approach produces much more intentional code as it is clear where a type is used and for what it is used.

You may say that a better solution to "scope" types is by splitting a module into multiple modules and only defining the type in the necessary module. But should TypeScript, a language, be this opinionated?

@jcalz
Copy link
Contributor

jcalz commented Nov 5, 2023

I suppose my example was a poor one

Could you edit the motivating example to include more motivation and fewer distractions?

I am proposing the ability to define types with deeper scopes, such as a function's scope.

Function-scoped types are already supported. Can you demonstrate otherwise? Or describe what about the current support is insufficient?

@RyanCavanaugh RyanCavanaugh added the Unactionable There isn't something we can do with this issue label Nov 6, 2023
@RyanCavanaugh
Copy link
Member

I am proposing the ability to define types with deeper scopes, such as a function's scope

Which you can, so what is this about? I don't see anything new, at all, being proposed here.

@RyanCavanaugh RyanCavanaugh closed this as not planned Won't fix, can't repro, duplicate, stale Nov 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Unactionable There isn't something we can do with this issue
Projects
None yet
Development

No branches or pull requests

5 participants