Skip to content

Meta-issue: Different type assertion behaviors #26064

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

Closed
RyanCavanaugh opened this issue Jul 30, 2018 · 3 comments
Closed

Meta-issue: Different type assertion behaviors #26064

RyanCavanaugh opened this issue Jul 30, 2018 · 3 comments
Labels
Meta-Issue An issue about the team, or the direction of TypeScript

Comments

@RyanCavanaugh
Copy link
Member

This is a disambiguator issue for tracking various type assertion-like constructs that have been requested

"is" type assertion

Tracking at #7481

Description: A type assertion operator that doesn't perform downcasting. This comes up reasonably often and is especially useful when you want to write an expression where the expression's target type is any/unknown but you know what type the expression ought to have.

// We want to validate that the object literal here is a Point3D,
// but the downcast here doesn't catch our miss of "z"
writeFile("point.json", JSON.stringify({ x: 2, y: 5 } _as_ Point3D);

This is also useful when writing function expressions or objects containing function expressions - its parameters can get contextually typed by the asserted-to type, but you really don't want downcasting of the return types to occur here.

#16605 tracks a different proposal for allowing a downcasting that downcasts the existence of missing properties but does not allow downcasting of types -- this is sort of ill-defined IMHO and needs some justification to exist as a separate entity.

"pass-through" type assertion

Tracking at #26045

Description: You currently have to choose between index signature validation at the initialization site or getting type checking on property existence:

type Neat = { [key: string]: boolean };

const x: Neat = {
    m: true,
    n: 10 // Want error here...
};
// ... and here
console.log(x.z);

The idea is some kind of annotation that still allows for inference, but doesn't change the type of the expression

Others

There are more to be found

@RyanCavanaugh RyanCavanaugh added the Meta-Issue An issue about the team, or the direction of TypeScript label Jul 30, 2018
@RyanCavanaugh
Copy link
Member Author

TODO describe #10421

@RyanCavanaugh
Copy link
Member Author

#26272 describes another use case for a "pass-through" assertion (simplified):

const a: Partial<Point> = { x: 10 };
// Error, 'x' might be undefined
console.log(a.x.toFixed());

Here we want to validate the initializer of a but not lose type information about which properties were initialized

@agentcooper
Copy link
Contributor

I'm finding the following behavior confusing:

type Fruit = "apple" | "banana"
([1, 2, 3] as Array<[Fruit]>); // OK: error is expected
(["apple", "potato"] as Array<Fruit>); // Not OK: no error

Plaground.

Should this considered as a bug?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Meta-Issue An issue about the team, or the direction of TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants