Skip to content

Commit fd9248d

Browse files
authored
fix: add loose auto complete to string literals where applicable (#966)
## What kind of change does this PR introduce? Add "loose autocomplete" to`WeakPasswordReasons`, `AMREntry.method`, `Factor.factor_type`, `SignInWithIdTokenCredentials.provider` and `AuthError.code ` ## What is the current behavior? The above types define possible string literal values, which is great, but they're unioned with `string`. As a regular `"literal" | string` just ends up as a `string` this foregoes the benefit of defining the literals in the first place. ## What is the new behavior? TypeScript still allows any possible string as a valid value (notice no error on the "not in there"), but now the predefined literals show up as hints. This is done by using `"literal" | string & {}`. While this seems hacky, it's something that works and something the TS team test against meaning they won't "fix" it in future releases. <img width="632" alt="image" src="https://github.com/user-attachments/assets/a6bd07c1-c460-4b80-87ae-2c855e996157"> Currently these hints aren't shown as the type just ends up as a simple `string`. ## Additional context I believe this can be quite helpful for people when trying to handle errors etc. I know `string & {}` might look somewhat weird for maintainers, if preferable I can create a `LooseAutocomplete<T>` or something and wrap them in there but this seemed cleaner.
1 parent 67d3e86 commit fd9248d

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

src/lib/errors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export class AuthError extends Error {
88
* before a response is received will not have one present. In that
99
* case {@link #status} will also be undefined.
1010
*/
11-
code: ErrorCode | string | undefined
11+
code: ErrorCode | (string & {}) | undefined
1212

1313
/** HTTP status code that caused the error. */
1414
status: number | undefined

src/lib/types.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export type GoTrueClientOptions = {
9494
hasCustomAuthorizationHeader?: boolean
9595
}
9696

97-
export type WeakPasswordReasons = 'length' | 'characters' | 'pwned' | string
97+
export type WeakPasswordReasons = 'length' | 'characters' | 'pwned' | (string & {})
9898
export type WeakPassword = {
9999
reasons: WeakPasswordReasons[]
100100
message: string
@@ -271,7 +271,7 @@ export interface Session {
271271
*/
272272
export interface AMREntry {
273273
/** Authentication method name. */
274-
method: 'password' | 'otp' | 'oauth' | 'mfa/totp' | string
274+
method: 'password' | 'otp' | 'oauth' | 'mfa/totp' | (string & {})
275275

276276
/**
277277
* Timestamp when the method was successfully used. Represents number of
@@ -310,7 +310,7 @@ export interface Factor {
310310
/**
311311
* Type of factor. `totp` and `phone` supported with this version
312312
*/
313-
factor_type: 'totp' | 'phone' | string
313+
factor_type: 'totp' | 'phone' | (string & {})
314314

315315
/** Factor's status. */
316316
status: 'verified' | 'unverified'
@@ -604,7 +604,7 @@ export type SignInWithOAuthCredentials = {
604604

605605
export type SignInWithIdTokenCredentials = {
606606
/** Provider name or OIDC `iss` value identifying which provider should be used to verify the provided token. Supported names: `google`, `apple`, `azure`, `facebook`, `kakao`, `keycloak` (deprecated). */
607-
provider: 'google' | 'apple' | 'azure' | 'facebook' | 'kakao' | string
607+
provider: 'google' | 'apple' | 'azure' | 'facebook' | 'kakao' | (string & {})
608608
/** OIDC ID token issued by the specified provider. The `iss` claim in the ID token must match the supplied provider. Some ID tokens contain an `at_hash` which require that you provide an `access_token` value to be accepted properly. If the token contains a `nonce` claim you must supply the nonce used to obtain the ID token. */
609609
token: string
610610
/** If the ID token contains an `at_hash` claim, then the hash of this value is compared to the value in the ID token. */

0 commit comments

Comments
 (0)