-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Closed
Closed
Copy link
Labels
BugA bug in TypeScriptA bug in TypeScriptFixedA PR has been merged for this issueA PR has been merged for this issue
Description
Bug Report
Getting lots of issues with a template string inference pattern I use in GitLens for creating strongly typed versions of string-based settings. And in TS 4.5-beta (and the currently nightly) I am getting Argument of type '* is not assignable to parameter of type
errors with that pattern
🔎 Search Terms
🕗 Version & Regression Information
- This is a crash
- This changed between versions
4.4.4
and4.5-beta
and still present in4.5.0-dev.202111101
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
- I was unable to test this on prior versions because _______
⏯ Playground Link
Playground link with relevant code
💻 Code
type SubPath<T, Key extends keyof T> = Key extends string
? T[Key] extends Record<string, any>
?
| `${Key}.${SubPath<T[Key], Exclude<keyof T[Key], keyof any[]>> & string}`
| `${Key}.${Exclude<keyof T[Key], keyof any[]> & string}`
: never
: never;
type Path<T> = SubPath<T, keyof T> | keyof T extends string | keyof T ? SubPath<T, keyof T> | keyof T : keyof T;
type PathValue<T, P extends Path<T>> = P extends `${infer Key}.${infer Rest}`
? Key extends keyof T
? Rest extends Path<T[Key]>
? PathValue<T[Key], Rest>
: never
: never
: P extends keyof T
? T[P]
: never;
export interface Config {
foo: {
bar: {
baz: boolean;
};
};
}
type ConfigPath = Path<Config>;
type ConfigPathValue<P extends ConfigPath> = PathValue<Config, P>;
export class Configuration {
get(): Config;
get<T extends ConfigPath>(
section: T,
defaultValue?: ConfigPathValue<T>,
): ConfigPathValue<T>;
get<T extends ConfigPath>(
section?: T,
defaultValue?: ConfigPathValue<T>,
): Config | ConfigPathValue<T> {
return undefined as any;
}
}
const configuration = new Configuration();
const value = configuration.get('foo.bar.baz'); // <-- Argument of type '"foo.bar.baz"' is not assignable to parameter of type '"foo"' in TS 4.5-beta
Output
export class Configuration {
get(section, defaultValue) {
return undefined;
}
}
const configuration = new Configuration();
const value = configuration.get('foo.bar.baz'); // <-- Argument of type '"foo.bar.baz"' is not assignable to parameter of type '"foo"' in TS 4.5-beta
Compiler Options
{
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true,
"strictBindCallApply": true,
"noImplicitThis": true,
"noImplicitReturns": true,
"alwaysStrict": true,
"esModuleInterop": true,
"declaration": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"target": "ES2017",
"jsx": "react",
"module": "ESNext",
"moduleResolution": "node"
}
}
🙁 Actual behavior
Argument of type '"foo.bar.baz"' is not assignable to parameter of type
🙂 Expected behavior
No errors like in TS 4.4.4 and prior
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptFixedA PR has been merged for this issueA PR has been merged for this issue