Comments (4)
Duplicate #33014
For now, if you need this behavior you either need to rewrite in terms of #47109 and use generic indexing instead of control flow anlaysis:
function func<T extends Type>(type: T, extend: X<T>) {
const x: { [K in Type]: (type: K, x: X<K>) => void } = {
[Type.A](type, extend) {
console.log(type, extend);
// ^? (parameter) extend: string
},
[Type.B](type, extend) {
}
}
x[type](type, extend);
}
func(Type.A, '');
func(Type.B, 1);
func(Math.random() < 0.99 ? Type.A : Type.B, 1); // still allowed, #47109 technically unsound this way
or, you need to use control flow analysis on a discriminated union and not use generics:
function func(...[type, extend]: { [T in Type]: [type: T, extend: X<T>] }[Type]) {
if (type === Type.A) {
console.log(type, extend);
// ^? (parameter) extend: string
}
}
func(Type.A, '');
func(Type.B, 1);
func(Math.random() < 0.99 ? Type.A : Type.B, 1); // error
from typescript.
Because checking what value type
is does not have any relevance for what type the value of extend
is.
This is a perfectly valid call: func<Type.A | Type.B>(Type.A, 1);
from typescript.
Because checking what value
type
is does not have any relevance for what type the value ofextend
is.This is a perfectly valid call:
func<Type.A | Type.B>(Type.A, 1);
How should I constrain the type of "extend" ? Do you have any suggestions ? Thank you so much !
from typescript.
Duplicate #33014
For now, if you need this behavior you either need to rewrite in terms of #47109 and use generic indexing instead of control flow anlaysis:
function func<T extends Type>(type: T, extend: X<T>) { const x: { [K in Type]: (type: K, x: X<K>) => void } = { [Type.A](type, extend) { console.log(type, extend); // ^? (parameter) extend: string }, [Type.B](type, extend) { } } x[type](type, extend); } func(Type.A, ''); func(Type.B, 1); func(Math.random() < 0.99 ? Type.A : Type.B, 1); // still allowed, #47109 technically unsound this wayor, you need to use control flow analysis on a discriminated union and not use generics:
function func(...[type, extend]: { [T in Type]: [type: T, extend: X<T>] }[Type]) { if (type === Type.A) { console.log(type, extend); // ^? (parameter) extend: string } } func(Type.A, ''); func(Type.B, 1); func(Math.random() < 0.99 ? Type.A : Type.B, 1); // error
Good job
from typescript.
Related Issues (20)
- `tsBuildInfoFile` compiler option should be allowed without `incremental` or `composite` in Typescript versions `>5.6`
- super() typed as returning void HOT 2
- Move to File does not generate imports in the same way that autoimports does
- Move To File makes extra spaces when moving jsdoc comments
- Type all exported members according to some interface HOT 2
- JSDoc typed function comments inside functions
- Error when accessing CSS property value using kebab case in `CSSStyleDeclaration` object HOT 1
- Type 'never' incorrectly inferred for mixed primitive type properties assignment with strictNullChecks disabled HOT 2
- Array of object unions not working as expected HOT 2
- Support WebIDL interfaces nominally (not just structurally) HOT 9
- Improve type inference for nullish coalescing (??) after null checks on multiple variables HOT 2
- [NewErrors] 5.7.0-dev.20240915 vs 5.6.2 HOT 6
- [ServerErrors][JavaScript] 5.7.0-dev.20240915 vs 5.6.2 HOT 3
- [ServerErrors][TypeScript] 5.7.0-dev.20240915 vs 5.6.2 HOT 4
- Inlay parameter hints don't account for trailing required parameters HOT 1
- JsDoc with overloads and different generics results in wrong dts output
- Generic Conditional Type resolved to `any` when passed an error `any` type HOT 8
- Typescript v5.6.2 resulting in "Overload signatures must all be optional or required." on alpine linux arm64 HOT 2
- File watching is registering thousands of redundant watchers
- File watching creates hundreds of recursive watchers in mono-repo when opening loose file
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.