-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
non-null assertion not working on IndexedAccess where K is a TypeParameter #21369
Comments
A general issue is that null assertion operator does not have any effect on generic type variables, that is type parameters, and indexed access types.. so With that in mind, previously, in TS 2.6 and before, the logic for getting a constraint on a indexed access type was if it has an index signature, then use that, otherwise it was In TS 2.7.1, the compiler is a bit smarter here, and will compute the constraint to be the union of all possible properties here, |
this is ridiculous, you made a special case at the price of breaking a fundamental concept, what is a top type that can fit |
I think this also affects me? export class InjectionError extends Error {
constructor(msg: string, public readonly dependency: string) {
super(msg);
}
}
export class DependencyNotFoundError extends InjectionError {
constructor(dependency: string) {
super(`${dependency} has not been provided`, dependency);
}
}
/**
* Indicates a circular depedency during injection.
*/
export class CircularInjectionError extends InjectionError {
constructor(dependency: string, public readonly dependencyStack: string[]) {
super(`Circular injection detected for ${dependency} ${dependencyStack.join('->')}`, dependency);
}
}
/**
* DI style dependency injector.
*/
export class Injector<T extends object> {
private injectMap: {
[K in keyof T]?: T[K];
} = {};
private provideMap: {
[K in keyof T]?: () => T[K];
} = {};
private injectStack: string[] = [];
/**
* Attempts to load a dependency from the injector.
* Will throw if the dependency is not present or a circular
* dependency was detected.
*/
public inject<K extends keyof T>(name: K): T[K] {
if (this.injectStack.includes(name)) {
const err = new CircularInjectionError(name, this.injectStack.slice());
this.injectStack = [];
throw err;
}
this.injectStack.push(name);
const inj = this.injectMap[name];
if (inj) {
this.injectStack.pop();
return inj; // < ~~~~~~~~~~~~~~~~~~~~ complains about potentially being undefied
}
const provider = this.provideMap[name];
if (!provider) {
this.injectStack = [];
throw new DependencyNotFoundError(name);
}
const ret = this.injectMap[name] = provider();
this.injectStack.pop();
return ret;
}
/**
* Adds a depedency for later injection.
*/
public provide<K extends keyof T>(name: K, val: () => T[K]) {
delete this.injectMap[name];
this.provideMap[name] = val;
}
} |
TypeScript Version: 2.7.0-dev.20180123
Search Terms: indexed access, non null assertion, type parameter
Code
Expected behavior:
No error as this worked very well in
typescript@2.7.0-rc
Actual behavior:
Error as stated in the inline comment.
Playground Link:
Related Issues:
Another recent change to IndexedAccess inference: #21368
The text was updated successfully, but these errors were encountered: