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

Add typeofEquals statements #32162

Closed
5 tasks done
Validark opened this issue Jun 28, 2019 · 3 comments
Closed
5 tasks done

Add typeofEquals statements #32162

Validark opened this issue Jun 28, 2019 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@Validark
Copy link

Validark commented Jun 28, 2019

Search Terms

typeof equals statements
typeof = statements
persistent as statement

Suggestion

I would like to see a new kind of statement added to TypeScript, called the typeofEquals statement. It would look like this:

function foo(bar: Type) {
    typeof bar = Type & {
        [K: string]: Type
    };

    bar.x // Type
    bar.y // Type
}

Use Cases

This would be used similarly to the as operator but would persist beyond a single usage. That way a developer can specify that a variable is a different type past a certain point, without declaring a new identity variable.

function f(o: Type) {
    // stuff
    // past this point `o` is guaranteed to be the below type
    // currently, one would need to declare a new variable
    let o2 = o as Type & {
        [K: string]: Type
    };

    // really hope nobody accidentally uses `o` instead of `o2` below this point,
    // especially since `o` and `o2` can be mutated!
}

Examples

Kind of a dumb example, but here is one:

interface Node<T> {
	Next: Node<T>;
	Value: T;
}

function foo<T>(node?: Node<T>) {
	const values = new Array<T>();
	let target = node && node.Next;

	while (target) {
		typeof node = Node<T>;
                // node is now Node<T> !!!
		const { Value: value } = node;
	}
}

Assigning to incompatible types

as operators error if you try to tell the compiler it is an incompatible type. The way to get around this is using as unknown as .... This could have something analogous. I would recommend a NonNullAssertion operator at the end of the type declaration being equivalent to overriding not being able to assign to an incompatible type.

let x = "4"
typeof x = 4!;

This would also provide a workaround for all problems with the type system improperly understanding variable types.
#30557
#31892

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, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@MartinJohns
Copy link
Contributor

Duplicate of #10421, #31512, #31879, #17760, #32130.

@fatcerberus
Copy link

One advantage this has over #32130 is that the CF graph doesn't explode with new nodes for every function call.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jun 28, 2019
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants