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

Cascade type narrowing to child constants #53202

Open
5 tasks done
drakuno opened this issue Mar 10, 2023 · 0 comments
Open
5 tasks done

Cascade type narrowing to child constants #53202

drakuno opened this issue Mar 10, 2023 · 0 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@drakuno
Copy link

drakuno commented Mar 10, 2023

Suggestion

πŸ” Search Terms

narrow child variable related

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Constants that were assigned a child value of another variable or constant should have their type narrowed alongside their parent.

πŸ“ƒ Motivating Example

Consider the following code:

type SomeUnion = (
  | {type: "string", values: readonly string[]}
  | {type: "number", values: readonly number[]}
  | {type: "date", values: readonly Date[]}
);

function someFunction(union:SomeUnion, index:number): void {
  const value = union.values[index];
  if (union.type === "string") {
    union.values; // readonly string[]
    value; // string | number | Date
  }
}

In someFunction, TypeScript keeps the type of value as it was originally assigned (string | number | Date).

What if dev expects value to have been narrowed to string?

This already happens with the root keys of an object

type AnotherUnion = (
    | {type: "string", sibling: string, foo: {bar: string}}
    | {type: "number", sibling: number, foo: {bar: number}}
    | {type: "date", sibling: Date, foo: {bar: Date}}
);

function manualDestructure(union: AnotherUnion): void {
  const {type, sibling, foo: {bar}} = union;
  if (type === "string") {
    sibling; // string βœ”οΈ 
    bar; // string | number | Date 😿 
  }
}

function argumentDestructure({type, sibling, foo: {bar}}: AnotherUnion): void {
  if (type === "string") {
    sibling; // string βœ”οΈ 
    bar; // string | number | Date 😿 
  }
}

This is observable in both examples.

πŸ’» Use Cases

Use case: Working with discriminated unions holding nested objects and arrays

To achieve such narrowing, one currently must adjust coding style. ie:

function adjustedScope(union:SomeUnion, index:number): void {
  if (union.type === "string") {
    const value = union.values[index];
    value; // string
  } else if (union.type === "number") {
    const value = union.values[index]; // 😭 
    value; // number
  }
}

// or

function typeAssertion(union:SomeUnion, index:number): void {
  const value = union.values[index];
  if (union.type === "string") {
    (value as string); // string
  } else if (union.type === "number") {
    (value as number); // number 😭 & risky?
  }
}
@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Mar 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants