microsoft / microsoft/TypeScript
Feature Request: In JS, check if the non-widened type of an object property satisfies type constraints
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
Search Terms
javascript as const, expando literal
Suggestion
I am providing declaration files that are mainly consumed by JS projects. The API I'm writing these declaration files for is very strict in what it accepts and I am mirroring that in the declarations. Users often stumble across assignments that seem perfectly valid, but are not accepted by TypeScript. The reason is the widening from literal types to string.
Here's a (dumbed down) example:
/** @param {{prop1: "a" | "b"}} arg */
function foo(arg) {}
const arg = {};
arg.prop1 = "a";
foo(arg); // error here: string is not compatible with "a" | "b".
const arg2 = {
prop1: "b"
};
foo(arg2); // Also an error
While in TypeScript, I can just put as const behind the assignment, there's no such thing in JS. Therefore I propose that when type-checking such constructs, the implicit literal type (here: prop1: "a", resp. prop1: "b") is taken into account.
Use Cases
Simplifying usage of strictly-typed libraries from JavaScript. Many JS users don't know what to do with those kinds of error messages and assume that there's a bug in the library or type definitions.
Examples
Here's how it could work:
const arg2 = {
prop1: "b"
};
foo(arg2); // should be fine, arg2.prop1 should be considered as literal `"b"` for this call
arg2.prop1 = "a";
foo(arg2); // should still be fine, CFA should know that prop1 is of type `"a"`
arg2.prop1 = someFunctionThatReturnsString();
foo(arg2); // should be an error, since prop1 can now be any string
arg2.prop1 = "a";
foo(arg2); // should be fine again, since the last assignment was the literal `"a"`
Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code - It would change existing behavior, but I would argue it is not breaking.
- 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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the JavaScript examples in the issue and compare the current diagnostics with the proposed behavior for literal property assignments. Define the type-checking rules for literal assignments, reassignment from a string-returning function, and control-flow narrowing; done means the examples produce the requested diagnostics without changing emitted JavaScript.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100