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

Type Assertion after Creating a Variable #56769

Closed
6 tasks done
mrjackyliang opened this issue Dec 13, 2023 · 3 comments
Closed
6 tasks done

Type Assertion after Creating a Variable #56769

mrjackyliang opened this issue Dec 13, 2023 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@mrjackyliang
Copy link

mrjackyliang commented Dec 13, 2023

🔍 Search Terms

type assert after creating a variable

✅ Viability Checklist

⭐ Suggestion

This is something I noticed when I was writing my Homebridge plugin:

// Create the new accessory without context.
const newAccessory = new this.#api.platformAccessory(
  device.name,
  device.hap.uuid,
  getAccessoryCategory(device.hap.category),
);

// Set the context into the new accessory.
newAccessory.context = device;

// Let TypeScript know that the context now exists. This creates additional runtime.
const typedAccessory = newAccessory as ADTPulsePlatformAddAccessoryTypedNewAccessory;

// Using the updated type constant.
this.#log.info(`Adding ${typedAccessory.context.name} (id: ${typedAccessory.context.id}, uuid: ${typedAccessory.context.hap.uuid}) accessory ...`);

The approach with this in the current TypeScript environment is valid, but I would like not to assign a new variable that creates additional runtime. At least I tried to search for something like this.

I can, however just set the type to the final type with the context, but that introduces an error when I started querying the context, which at that time was undefined.

📃 Motivating Example

I believe this fits into the goals of TypeScript because it helps create a more type-safe environment.

💻 Use Cases

  1. What do you want to use this for?
    I'm using this in situations where I cannot account for types being assigned at a particular time.

  2. What shortcomings exist with current approaches?
    I can either pre-define the type with a filled-in context type (which may cause runtime crashes), or create a new constant and type assert that (what I'm doing now).

  3. What workarounds are you using in the meantime?
    Same as bullet point 2.

@fatcerberus
Copy link

fatcerberus commented Dec 13, 2023

I don’t know what you mean by “create additional runtime” but I gather it’s something to do with runtime type info which is out of scope for TS (type information is intentionally erased by the compiler and has no effect on runtime behavior), hence these two bullet points which you probably shouldn’t have checked:

* 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.)

Disregard the above; I misread the issue.

@jcalz
Copy link
Contributor

jcalz commented Dec 13, 2023

Looks like a duplicate of #10421

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Dec 13, 2023
@mrjackyliang
Copy link
Author

Oh sorry! This is a duplicate.

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

4 participants