Comments (6)
FWIW, I don't think computed property names are compatible with isolatedDeclarations
at a fundamental level - wherever we land on the fix is going to be a compromise. Computed names are, by their nature, a window from the type structure world into nonlocal expression checking space. I'm willing to say "we do the right thing when all the information is in the file" for the sake of convenience, since when the information isn't in the file, even a simple [key]: whatever
is completely unsafe and not transformable into a type that's guaranteed to work the same way as the input implies. A export const a: {[expr]: number}
does not have the same behavior as a export const a = {[expr]: 1}
, and the differences all come down to the type of expr
. As an expression, we allow expr
to be string
, any
- any number of non-literal things. And depending on context, those are either elided entirely, transformed into index signatures, or maybe even splat out into multiple optional properties. In a type, expr
must typecheck as a property name literal type to be valid (not a union thereof, not a PropertyKey
extending param, exactly a literal type). Otherwise we issue a A computed property name in a type literal must refer to an expression whose type is a literal type or a 'unique symbol' type.
error when we read the type back in.
When it comes down to it, there's no simple rule to transform from an expression computed name to a type one that actually works (without full potentially nonlocal context). Any "rule" we apply is heuristic, at best. :S
Anyways, also ref #58490 which is on the same subject.
from typescript.
@weswigham I agree with you - however I think ID already handles this on a checker level, because the checker disallows any types in computed key expressions that are not string literals, number literals, or unique symbol
- types that would result in type emitted output that is not a property member, but a index member or similar are already forbidden (number
, string
, symbol
). See https://www.typescriptlang.org/play/?isolatedDeclarations=true&ts=5.5.0-dev.20240514#code/KYDwDg9gTgLgBAYwgOwM7wNLAJ4EYBcc6UAlsgOYDcAUKJLIiunFtgEyHICuAtgEbAoNOtHhI0mHAGZCqbPwgAbarXCjGEuAEE4AXjgBvanBNwA2q1wBdQjChdgAGmOmLONjbh2Hz0+dZSnt5O1AC+1EA
This issue was not about changing the checker, just about changing the emit behaviour specifically for the allowed types (string literals, number literals, or unique symbol
), to retain the original expression rather than replacing it with the type value.
There is no semantic issue here as far as I can tell, it's just something that is needed to achieve token level equality of the output.
from typescript.
There is no semantic issue here as far as I can tell, it's just something that is needed to achieve token level equality of the output.
ish? The ID error can't be correctly emitted without full type information (see #58490), so you can't know if it's even safe to copy an expression computed name to a type computed name without doing whole-program typechecking. So while ID does have an error for this, it fails to emit it under the transpileDeclaration
API (and just allows a potentially very wrong transform).
from typescript.
Yeah but that's already the case for other transforms, no? My understanding was that isolated declarations always operates on the assumption that the code is valid and does type check. Maybe I am misunderstanding this.
from typescript.
I retract my previous comment. I don't think it is reasonable for the emitter to have to make the assumption that the ID checker passed. Doing so would make it impossible to analyze without TSC's ID checker whether an arbitrary given file is eligible for ID emit in the first place, which makes the performance gains quite useless. You would still have to check all files anyway to determine whether you can ID emit them before emit.
from typescript.
These are all errors for now, but as we allow some of these forms as special cases, we do want to think about possibly aligning the emit output where possible, so this'll track that.
from typescript.
Related Issues (20)
- CommonJS globals permitted for ES module builds with no compiler error. HOT 4
- -
- Type predicate doesn't narrow down function overload (but assertion works) HOT 3
- Recursive type strangely resolved to never HOT 3
- [5.5 beta] The `moduleDetection` option is ignored for `verbatimModuleSyntax` error HOT 6
- [NewErrors] 5.5.0-dev.20240526 vs 5.4.5 HOT 63
- [ServerErrors][JavaScript] 5.5.0-dev.20240526 HOT 11
- [ServerErrors][TypeScript] 5.5.0-dev.20240526 HOT 20
- Type Error in Array.includes() method when the array contains only string keys
- Type Error in Array.includes() method when the array contains only string keys HOT 4
- Attribute and Method Visibility depending on generics in form of `private` return type HOT 3
- String template literals as object keys don't get validated correctly HOT 4
- WebWorker NotificationOptions type issue HOT 1
- Type a standard import with jsdoc only HOT 3
- tsc: "Cannot read properties of undefined (reading 'parent')" HOT 2
- Incorrect intersection for calced constructor HOT 1
- Assignation of incorrect value doesn't type error HOT 1
- TS 5.5.0-beta Regression: generated .d.ts is not compatible HOT 2
- [isolatedDeclarations] Should imported JSON files be emitted when "resolveJsonModules", "emitDeclarationOnly", and "isolatedDeclarations" are all enabled?
- Google feedback on TS 5.5-beta HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typescript.